xref: /btstack/test/gatt_service_server/battery_service_server_test.cpp (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
1 
2 // *****************************************************************************
3 //
4 // test battery service
5 //
6 // *****************************************************************************
7 
8 
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include "CppUTest/TestHarness.h"
15 #include "CppUTest/CommandLineTestRunner.h"
16 #include "CppUTestExt/MockSupport.h"
17 
18 #include "hci.h"
19 #include "btstack_util.h"
20 #include "bluetooth.h"
21 #include "bluetooth_gatt.h"
22 
23 #include "ble/gatt-service/battery_service_server.h"
24 #include "battery_service_server_test.h"
25 #include "mock_att_server.h"
26 
27 static uint8_t battery_level = 100;
28 
29 TEST_GROUP(BATTERY_SERVICE_SERVER){
30     att_service_handler_t * service;
31     uint16_t con_handle;
32     uint16_t battery_value_handle_value;
33     uint16_t battery_value_handle_client_configuration;
34 
35     void setup(void){
36         // setup database
37         att_set_db(profile_data);
38         battery_value_handle_value = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
39         battery_value_handle_client_configuration = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
40 
41         // setup battery service
42         battery_service_server_init(battery_level);
43 
44         service = mock_att_server_get_service();
45         con_handle = 0x00;
46     }
47 
48     void teardown(){
49         mock_deinit();
50     }
51 };
52 
53 TEST(BATTERY_SERVICE_SERVER, set_battery_value_trigger_can_send_now){
54     // enable notifications
55     const uint8_t enable_notify[]= { 0x1, 0x0 };
56     mock_att_service_write_callback(con_handle, battery_value_handle_client_configuration, ATT_TRANSACTION_MODE_NONE, 0, enable_notify, sizeof(enable_notify));
57 
58     // set battery to trigger notification
59     mock().expectOneCall("att_server_register_can_send_now_callback");
60     battery_service_server_set_battery_value(60);
61     mock().checkExpectations();
62     mock().expectOneCall("att_server_notify");
63     mock_att_service_trigger_can_send_now();
64     mock().checkExpectations();
65 }
66 
67 TEST(BATTERY_SERVICE_SERVER, lookup_attribute_handles){
68     CHECK(battery_value_handle_value != 0);
69     CHECK(battery_value_handle_client_configuration != 0);
70 }
71 
72 
73 TEST(BATTERY_SERVICE_SERVER, set_battery_value){
74     // battery_value_handle_client_configuration not set
75     mock().expectNCalls(con_handle, "att_server_register_can_send_now_callback");
76     battery_service_server_set_battery_value(60);
77     mock().checkExpectations();
78 
79     // battery_value_handle_client_configuration set
80     mock().expectOneCall("att_server_register_can_send_now_callback");
81     const uint8_t enable_notify[]= { 0x1, 0x0 };
82     mock_att_service_write_callback(con_handle, battery_value_handle_client_configuration, ATT_TRANSACTION_MODE_NONE, 0, enable_notify, sizeof(enable_notify));
83     battery_service_server_set_battery_value(60);
84     mock().checkExpectations();
85 }
86 
87 TEST(BATTERY_SERVICE_SERVER, set_wrong_handle_battery_value){
88     // battery_value_handle_client_configuration not set
89     mock().expectNCalls(con_handle, "att_server_register_can_send_now_callback");
90     battery_service_server_set_battery_value(60);
91     mock().checkExpectations();
92 
93     // battery_value_handle_client_configuration set
94     const uint8_t enable_notify[]= { 0x1, 0x0 };
95 
96     mock().expectNoCall("att_server_register_can_send_now_callback");
97     mock_att_service_write_callback(con_handle, battery_value_handle_client_configuration + 10, ATT_TRANSACTION_MODE_NONE, 0, enable_notify, sizeof(enable_notify));
98     battery_service_server_set_battery_value(60);
99     mock().checkExpectations();
100 
101     mock().expectNoCall("att_server_register_can_send_now_callback");
102     mock_att_service_write_callback(con_handle, battery_value_handle_client_configuration + 10, ATT_TRANSACTION_MODE_VALIDATE, 0, enable_notify, sizeof(enable_notify));
103     battery_service_server_set_battery_value(60);
104     mock().checkExpectations();
105 }
106 
107 
108 
109 TEST(BATTERY_SERVICE_SERVER, read_battery_value){
110     uint8_t response[2];
111     uint16_t response_len;
112 
113     // invalid attribute handle
114     response_len = mock_att_service_read_callback(con_handle, 0xffff, 0xffff, response, sizeof(response));
115     CHECK_EQUAL(0, response_len);
116 
117     response_len = mock_att_service_read_callback(con_handle, battery_value_handle_value, 0, response, sizeof(response));
118     CHECK_EQUAL(1, response_len);
119 
120     response_len = mock_att_service_read_callback(con_handle, battery_value_handle_client_configuration, 0, response, sizeof(response));
121     CHECK_EQUAL(2, response_len);
122 }
123 
124 
125 int main (int argc, const char * argv[]){
126     return CommandLineTestRunner::RunAllTests(argc, argv);
127 }
128