1 /**
2 ****************************************************************************************
3 *
4 * @file app_user.c
5 *
6 * @brief Health Thermometer Application entry point
7 *
8 * Copyright (C) RivieraWaves 2009-2015
9 *
10 *
11 ****************************************************************************************
12 */
13 
14 /**
15  ****************************************************************************************
16  * @addtogroup APP
17  * @{
18  ****************************************************************************************
19  */
20 
21 #include "rwip_config.h"     // SW configuration
22 #include "n32wb452_ble_api.h"
23 #if (BLE_APP_USER)
24 
25 /*
26  * INCLUDE FILES
27  ****************************************************************************************
28  */
29 
30 #include "app_user.h"                  // Health Thermometer Application Definitions
31 #include "app.h"                     // Application Definitions
32 #include "app_task.h"                // application task definitions
33 
34 #include "co_bt.h"
35 #include "prf_types.h"
36 #include "prf_utils.h"
37 #include "ble_arch.h"                    // Platform Definitions
38 #include "gapm_task.h"
39 
40 #include "co_math.h"
41 #include "ke_timer.h"
42 #include "prf.h"
43 #include "user_task.h"
44 #include "user.h"
45 #include "co_utils.h"
46 #include "gattc.h"
47 #include "gapm.h"
48 //#include "interface.h"
49 
50 #include "co_math.h"
51 
52 
53 #if (DISPLAY_SUPPORT)
54 #include "app_display.h"
55 #include "display.h"
56 #endif //DISPLAY_SUPPORT
57 //#include "Bkey_user.h"
58 
59 #define DEMO_APP
60 
61 
62 
63 //uint8_t whitelistsize = 0;
64 
65 //INT8 rssivalue = 0;
66 /*
67  * GLOBAL VARIABLE DEFINITIONS
68  ****************************************************************************************
69  */
70 struct app_user_rev_tag app_user_env;
71 /*
72  * LOCAL FUNCTION DEFINITIONS
73  ****************************************************************************************
74  */
75 uint32_t need_rsp_type = 0;
app_user_init(void)76 void app_user_init(void)
77 {
78     memset(&app_user_env, 0, sizeof(app_user_env));
79     ke_timer_set(GAPM_DEVICE_READY_IND, TASK_APP, 10); //10M后复位
80 }
81 
82 
83 
add_users(uint16_t prf_task_id)84 void add_users(uint16_t prf_task_id)
85 {
86     struct user_db_cfg *db_cfg;
87 
88     struct gapm_profile_task_add_cmd *req = KE_MSG_ALLOC_DYN(GAPM_PROFILE_TASK_ADD_CMD,TASK_GAPM, TASK_APP,gapm_profile_task_add_cmd, sizeof(struct user_db_cfg));
89     // Fill message
90     req->operation = GAPM_PROFILE_TASK_ADD;
91     req->sec_lvl =  PERM(SVC_AUTH, DISABLE);
92     req->prf_task_id = prf_task_id;
93     req->app_task = TASK_APP;
94     req->start_hdl = 0;
95 
96     // Set parameters
97     db_cfg = (struct user_db_cfg *) req->param;
98     //
99     db_cfg->features = (USER_READ_WRITE_CHAR_SUP | USER_DATA_NOTIFY_CHAR_SUP | USER_DATA_WRITE_CHAR_SUP);
100     db_cfg->txlen = USER_DATA_LEN_MAX;
101     db_cfg->rxlen = USER_DATA_LEN_MAX;
102 
103     // Send the message
104     ke_msg_send(req);
105 }
106 
app_user_add_users(void)107 void app_user_add_users(void)
108 {
109     add_users(TASK_ID_USER);
110 }
111 
data_notify_onePackage(uint16_t param_length,uint8_t * param_payload,uint16_t task_id,uint8_t att_idx)112 static uint8_t data_notify_onePackage(uint16_t param_length , uint8_t *param_payload, uint16_t task_id, uint8_t att_idx)
113 {
114     struct user_notify_req *req = KE_MSG_ALLOC_DYN(USER_NOTIFY_REQ,
115                                   prf_get_task_from_id(task_id),
116                                   TASK_APP,
117                                   user_notify_req, param_length);
118     req->send_param_length = param_length;
119     req->att_idx = att_idx;
120     memcpy(req->send_param_payload, param_payload, param_length);
121     ke_msg_send(req);
122     for(int i=0; i<8; i++)
123     {
124         BT_handle();
125     }
126     return 0;
127 }
data_notify(uint16_t param_length,uint8_t * param_payload,uint16_t task_id,uint8_t att_idx)128 uint8_t data_notify(uint16_t param_length , uint8_t *param_payload, uint16_t task_id, uint8_t att_idx)
129 {
130     uint16_t mtu_num = 0;
131     mtu_num = gattc_get_mtu(app_env.conidx) - 3;
132     while (param_length)
133     {
134         struct user_notify_req *req = KE_MSG_ALLOC_DYN(USER_NOTIFY_REQ,
135                                       prf_get_task_from_id(task_id),
136                                       TASK_APP,
137                                       user_notify_req, mtu_num);
138 
139 
140 
141         if (param_length > mtu_num)
142         {
143             req->send_param_length = mtu_num;
144             memcpy(req->send_param_payload, param_payload, mtu_num);
145             req->att_idx = att_idx;
146             ke_msg_send(req);
147             param_length -= mtu_num;
148             param_payload += mtu_num;
149         }
150         else
151         {
152             req->send_param_length = param_length;
153             req->att_idx = att_idx;
154             memcpy(req->send_param_payload, param_payload, param_length);
155             ke_msg_send(req);
156             param_length = 0;
157         }
158     }
159     return 1;
160 
161 }
162 
163 #if 0
164 uint8_t app_user_data_notify(uint16_t param_length , uint8_t *param_payload, uint8_t att_idx)
165 {
166     data_notify(param_length, param_payload, TASK_ID_USER, att_idx);
167     return 0;
168 }
169 #else
app_user_data_notify(uint16_t param_length,uint8_t * param_payload,uint8_t att_idx)170 uint8_t app_user_data_notify(uint16_t param_length , uint8_t *param_payload, uint8_t att_idx)
171 {
172     uint16_t mtu_num = 0;
173     mtu_num = gattc_get_mtu(app_env.conidx) - 3;
174     while (param_length)
175     {
176         if (param_length > mtu_num)
177         {
178             data_notify_onePackage(mtu_num , param_payload, TASK_ID_USER, att_idx);
179             param_length -= mtu_num;
180             param_payload += mtu_num;
181         }
182         else
183         {
184             data_notify_onePackage(param_length , param_payload, TASK_ID_USER, att_idx);
185             param_length = 0;
186         }
187     }
188     return 0;
189 }
190 #endif
191 
192 
193 // static uint8_t Speed_buf[7] ;
194 //void app_user_mtu_temp_send(uint16_t mtu)
195 //{
196 
197 //
198 
199 //    Speed_buf[0] = 0x05;
200 
201 //    Speed_buf[1] = 0x00;
202 
203 //    Speed_buf[2] = 0xAA;
204 
205 //    Speed_buf[3] = 0xBB;
206 
207 //    Speed_buf[4] = 0x00;
208 
209 //    Speed_buf[5] = mtu % 256;
210 
211 //    Speed_buf[6] = mtu / 256;
212 
213 //    app_user_data_notify(7, Speed_buf);
214 //}
215 
216 //static uint8_t data_buf[13] ;
217 //void app_user_updata_temp_send(uint8_t status)//,uint16_t intv_min, uint16_t intv_max, uint16_t latancy, uint16_t timesout)
218 //{
219 
220 //
221 
222 //    data_buf[0] = 0x0B;
223 
224 //    data_buf[1] = 0x00;
225 
226 //    data_buf[2] = 0xAA;
227 
228 //    data_buf[3] = 0xCC;
229 
230 //    data_buf[4] = status;
231 
232 //    data_buf[5] = app_env.con_intv_min % 256;
233 //    data_buf[6] = app_env.con_intv_min / 256;
234 //    data_buf[7] = app_env.con_intv_max % 256;
235 //    data_buf[8] = app_env.con_intv_max / 256;
236 //    data_buf[9] =  app_env.con_latency % 256;
237 //    data_buf[10] = app_env.con_latency / 256;
238 //    data_buf[11] = app_env.con_time_out % 256;
239 //    data_buf[12] = app_env.con_time_out / 256;
240 
241 //    app_user_data_notify(13, data_buf);
242 //}
243 
244 
245 
246 
247 
248 /**
249  ****************************************************************************************
250  * @brief Handles health thermometer timer
251  *
252  * @param[in] msgid     Id of the message received.
253  * @param[in] param     Pointer to the parameters of the message.
254  * @param[in] dest_id   ID of the receiving task instance (TASK_GAP).
255  * @param[in] src_id    ID of the sending task instance.
256  *
257  * @return If the message was consumed or not.
258  ****************************************************************************************
259  */
app_user_msg_handler(ke_msg_id_t const msgid,void const * param,ke_task_id_t const dest_id,ke_task_id_t const src_id)260 static int app_user_msg_handler(ke_msg_id_t const msgid,
261                                 void const *param,
262                                 ke_task_id_t const dest_id,
263                                 ke_task_id_t const src_id)
264 {
265     // todo
266 
267     return (KE_MSG_CONSUMED);
268 }
269 
270 /*
271  * LOCAL VARIABLE DEFINITIONS
272  ****************************************************************************************
273  */
274 
275 
276 /// Default State handlers definition
277 const struct ke_msg_handler app_user_msg_handler_list[] =
278 {
279     // Note: first message is latest message checked by kernel so default is put on top.
280     {KE_MSG_DEFAULT_HANDLER, (ke_msg_func_t)app_user_msg_handler},
281 
282 };
283 
284 const struct ke_state_handler app_user_table_handler =
285 {&app_user_msg_handler_list[0], (sizeof(app_user_msg_handler_list) / sizeof(struct ke_msg_handler))};
286 
287 #endif //BLE_APP_USER
288 
289 /// @} APP
290