xref: /btstack/test/gatt_service_server/battery_service_server_test.cpp (revision da8e14c5aa3783b6bb7dd63e71572a901bcf168b)
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     mock().expectNoCall("att_server_register_can_send_now_callback");
95     const uint8_t enable_notify[]= { 0x1, 0x0 };
96     mock_att_service_write_callback(con_handle, battery_value_handle_client_configuration + 10, ATT_TRANSACTION_MODE_NONE, 0, enable_notify, sizeof(enable_notify));
97     battery_service_server_set_battery_value(60);
98     mock().checkExpectations();
99 }
100 
101 
102 TEST(BATTERY_SERVICE_SERVER, read_battery_value){
103     uint8_t response[2];
104     uint16_t response_len;
105 
106     // invalid attribute handle
107     response_len = mock_att_service_read_callback(con_handle, 0xffff, 0xffff, response, sizeof(response));
108     CHECK_EQUAL(0, response_len);
109 
110     response_len = mock_att_service_read_callback(con_handle, battery_value_handle_value, 0, response, sizeof(response));
111     CHECK_EQUAL(1, response_len);
112 
113     response_len = mock_att_service_read_callback(con_handle, battery_value_handle_client_configuration, 0, response, sizeof(response));
114     CHECK_EQUAL(2, response_len);
115 }
116 
117 
118 int main (int argc, const char * argv[]){
119     return CommandLineTestRunner::RunAllTests(argc, argv);
120 }
121