_code/posts/tdd-intro/cmock-tutorial/ExampleCode/code/src/sensors_service.c
/* Initialization function for My Custom Service (sensors).*/
Initialization function for My Custom Service (sensors).
Initialization function for My Custom Service ( sensors ) .
ble_service_t *sensors_init(const sensors_service_cb_t *cb)
{
        sensors_service_t *ss;
        uint16_t num_attr;
        att_uuid_t uuid;
        uint16_t temp_char_user_descriptor_h;
        uint16_t acc_char_user_descriptor_h;
        ss = (sensors_service_t *)OS_MALLOC(sizeof(*ss));
        memset(ss, 0, sizeof(*ss));
        ss->svc.read_req  = handle_read_req;
        ss->svc.cleanup   = cleanup;
        ss->cb = cb;
        num_attr = ble_gatts_get_num_attr(0, 2, 2);
        ble_uuid_from_string("00000000-1111-2222-2222-333333333333", &uuid);
        ble_gatts_add_service(&uuid, GATT_SERVICE_PRIMARY, num_attr);
        ble_uuid_from_string("11111111-0000-0000-0000-111111111111", &uuid);
        ble_gatts_add_characteristic(&uuid, GATT_PROP_READ,  ATT_PERM_READ, 
                            1, GATTS_FLAG_CHAR_READ_REQ, NULL, &ss->temp_int_value_h);
        ble_uuid_create16(UUID_GATT_CHAR_USER_DESCRIPTION, &uuid);
        ble_gatts_add_descriptor(&uuid, ATT_PERM_READ, sizeof(temp_user_descriptor_val),
                                                              0, &temp_char_user_descriptor_h);
        ble_uuid_from_string("22222222-0000-0000-0000-222222222222", &uuid);
        ble_gatts_add_characteristic(&uuid, GATT_PROP_READ,  ATT_PERM_READ, 
                            1, GATTS_FLAG_CHAR_READ_REQ, NULL, &ss->acc_int_value_h);
        ble_uuid_create16(UUID_GATT_CHAR_USER_DESCRIPTION, &uuid);
        ble_gatts_add_descriptor(&uuid, ATT_PERM_READ, sizeof(acc_user_descriptor_val),
                                                              0, &acc_char_user_descriptor_h);
        ble_gatts_register_service(&ss->svc.start_h, &ss->temp_int_value_h, &ss->acc_int_value_h,
                          &temp_char_user_descriptor_h, &acc_char_user_descriptor_h ,0);
        ss->svc.end_h = ss->svc.start_h + num_attr;
        const uint8_t initial_value = 0;
        ble_gatts_set_value(ss->temp_int_value_h, 1,  &initial_value);
        ble_gatts_set_value(ss->acc_int_value_h, 1, &initial_value);
        ble_gatts_set_value(temp_char_user_descriptor_h,  sizeof(temp_user_descriptor_val),
                                                               temp_user_descriptor_val);
        ble_gatts_set_value(acc_char_user_descriptor_h,  sizeof(acc_user_descriptor_val),
                                                               acc_user_descriptor_val);
        ble_service_add(&ss->svc);
        return &ss->svc;
}
ble_service_t * sensors_init ( const sensors_service_cb_t * cb ) { sensors_service_t * ss ; uint16_t num_attr ; att_uuid_t uuid ; uint16_t temp_char_user_descriptor_h ; uint16_t acc_char_user_descriptor_h ; ss = ( sensors_service_t * ) OS_MALLOC ( sizeof ( * ss ) ) ; memset ( ss , 0 , sizeof ( * ss ) ) ; ss -> svc . read_req = handle_read_req ; ss -> svc . cleanup = cleanup ; ss -> cb = cb ; num_attr = ble_gatts_get_num_attr ( 0 , 2 , 2 ) ; ble_uuid_from_string ( " " , & uuid ) ; ble_gatts_add_service ( & uuid , GATT_SERVICE_PRIMARY , num_attr ) ; ble_uuid_from_string ( " " , & uuid ) ; ble_gatts_add_characteristic ( & uuid , GATT_PROP_READ , ATT_PERM_READ , 1 , GATTS_FLAG_CHAR_READ_REQ , NULL , & ss -> temp_int_value_h ) ; ble_uuid_create16 ( UUID_GATT_CHAR_USER_DESCRIPTION , & uuid ) ; ble_gatts_add_descriptor ( & uuid , ATT_PERM_READ , sizeof ( temp_user_descriptor_val ) , 0 , & temp_char_user_descriptor_h ) ; ble_uuid_from_string ( " " , & uuid ) ; ble_gatts_add_characteristic ( & uuid , GATT_PROP_READ , ATT_PERM_READ , 1 , GATTS_FLAG_CHAR_READ_REQ , NULL , & ss -> acc_int_value_h ) ; ble_uuid_create16 ( UUID_GATT_CHAR_USER_DESCRIPTION , & uuid ) ; ble_gatts_add_descriptor ( & uuid , ATT_PERM_READ , sizeof ( acc_user_descriptor_val ) , 0 , & acc_char_user_descriptor_h ) ; ble_gatts_register_service ( & ss -> svc . start_h , & ss -> temp_int_value_h , & ss -> acc_int_value_h , & temp_char_user_descriptor_h , & acc_char_user_descriptor_h , 0 ) ; ss -> svc . end_h = ss -> svc . start_h + num_attr ; const uint8_t initial_value = 0 ; ble_gatts_set_value ( ss -> temp_int_value_h , 1 , & initial_value ) ; ble_gatts_set_value ( ss -> acc_int_value_h , 1 , & initial_value ) ; ble_gatts_set_value ( temp_char_user_descriptor_h , sizeof ( temp_user_descriptor_val ) , temp_user_descriptor_val ) ; ble_gatts_set_value ( acc_char_user_descriptor_h , sizeof ( acc_user_descriptor_val ) , acc_user_descriptor_val ) ; ble_service_add ( & ss -> svc ) ; return & ss -> svc ; }
Initialization function for My Custom Service (sensors).
Initialization function for My Custom Service ( sensors ) .
/* Allocate memory for the sevice hanle */ /* Declare handlers for specific BLE events */ /*
         * 0 --> Number of Included Services
         * 2 --> Number of Characteristic Declarations
         * 2 --> Number of Descriptors
         */ /* Service declaration */ /* Characteristic declaration for Temperature sensor*/ /* Define descriptor of type Characteristic User Description (CUD) */ /* Characteristic declaration for accelerometer */ /* Define descriptor of type Characteristic User Description (CUD) */ /*
         * Register all the attribute handles so that they can be updated
         * by the BLE manager automatically.
         */ /* Calculate the last attribute handle of the BLE service */ /* Set default attribute values */ /* Register the BLE service in BLE framework */ /* Return the service handle */