ota_service.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. #include "bcomdef.h"
  29. #include "OSAL.h"
  30. #include "linkdb.h"
  31. #include "att.h"
  32. #include "gatt.h"
  33. #include "gatt_uuid.h"
  34. #include "gatt_profile_uuid.h"
  35. #include "peripheral.h"
  36. #include "gattservapp.h"
  37. #include "ota_service.h"
  38. #include "log.h"
  39. #ifndef CFG_OTA_MESH
  40. static const uint8 ota_ServiceUUID[ATT_UUID_SIZE] =
  41. { 0x23, 0xf1, 0x6e, 0x53, 0xa4, 0x22, 0x42, 0x61, 0x91, 0x51, 0x8b, 0x9b, 0x01, 0xff, 0x33, 0x58};
  42. //command characteristic
  43. static const uint8 ota_CommandUUID[ATT_UUID_SIZE] =
  44. { 0x23, 0xf1, 0x6e, 0x53, 0xa4, 0x22, 0x42, 0x61, 0x91, 0x51, 0x8b, 0x9b, 0x02, 0xff, 0x33, 0x58};
  45. // Sensor location characteristic
  46. static const uint8 ota_ResponseUUID[ATT_UUID_SIZE] =
  47. { 0x23, 0xf1, 0x6e, 0x53, 0xa4, 0x22, 0x42, 0x61, 0x91, 0x51, 0x8b, 0x9b, 0x03, 0xff, 0x33, 0x58};
  48. // Command characteristic
  49. static const uint8 ota_DataUUID[ATT_UUID_SIZE] =
  50. { 0x23, 0xf1, 0x6e, 0x53, 0xa4, 0x22, 0x42, 0x61, 0x91, 0x51, 0x8b, 0x9b, 0x04, 0xff, 0x33, 0x58};
  51. #else
  52. static const uint8 ota_ServiceUUID[ATT_UUID_SIZE] =
  53. { 0xde, 0x61, 0x49, 0x67, 0xea, 0x50, 0x57, 0x2c, 0xbb, 0xba, 0x52, 0x9b, 0x01, 0xff, 0x33, 0x58};
  54. //command characteristic
  55. static const uint8 ota_CommandUUID[ATT_UUID_SIZE] =
  56. { 0xde, 0x61, 0x49, 0x67, 0xea, 0x50, 0x57, 0x2c, 0xbb, 0xba, 0x52, 0x9b, 0x02, 0xff, 0x33, 0x58};
  57. // Sensor location characteristic
  58. static const uint8 ota_ResponseUUID[ATT_UUID_SIZE] =
  59. { 0xde, 0x61, 0x49, 0x67, 0xea, 0x50, 0x57, 0x2c, 0xbb, 0xba, 0x52, 0x9b, 0x03, 0xff, 0x33, 0x58};
  60. // Command characteristic
  61. static const uint8 ota_DataUUID[ATT_UUID_SIZE] =
  62. { 0xde, 0x61, 0x49, 0x67, 0xea, 0x50, 0x57, 0x2c, 0xbb, 0xba, 0x52, 0x9b, 0x04, 0xff, 0x33, 0x58};
  63. #endif
  64. static const gattAttrType_t ota_Service = { ATT_UUID_SIZE, ota_ServiceUUID };
  65. static uint8 ota_CommandProps = GATT_PROP_WRITE;
  66. static uint8 ota_CommandValue = 0;
  67. // OTA response Characteristic
  68. static uint8 ota_ResponseProps = GATT_PROP_NOTIFY;
  69. static uint8 ota_ResponseValue = 0;
  70. static gattCharCfg_t ota_ResponseCCCD[GATT_MAX_NUM_CONN];
  71. // OTA Data Characteristic
  72. static uint8 ota_DataProps = GATT_PROP_WRITE_NO_RSP;
  73. static uint8 ota_DataValue = 0;
  74. #define OTA_COMMAND_HANDLE 2
  75. #define OTA_RSP_HANDLE 4
  76. #define OTA_DATA_HANDLE 7
  77. static gattAttribute_t ota_AttrTbl[] =
  78. {
  79. //OTA Service
  80. {
  81. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  82. GATT_PERMIT_READ, /* permissions */
  83. 0, /* handle */
  84. (uint8 *)&ota_Service /* pValue */
  85. },
  86. //OTA Command Declaration
  87. {
  88. { ATT_BT_UUID_SIZE, characterUUID },
  89. GATT_PERMIT_READ,
  90. 0,
  91. &ota_CommandProps
  92. },
  93. //OTA Command Value
  94. {
  95. { ATT_UUID_SIZE, ota_CommandUUID },
  96. GATT_PERMIT_WRITE,
  97. 0,
  98. &ota_CommandValue
  99. },
  100. // OTA response Declaration
  101. {
  102. { ATT_BT_UUID_SIZE, characterUUID },
  103. GATT_PERMIT_READ,
  104. 0,
  105. &ota_ResponseProps
  106. },
  107. //response Value
  108. {
  109. { ATT_UUID_SIZE, ota_ResponseUUID },
  110. GATT_PERMIT_READ,
  111. 0,
  112. &ota_ResponseValue
  113. },
  114. // OTA response Client Characteristic Configuration
  115. {
  116. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  117. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  118. 0,
  119. (uint8 *) ota_ResponseCCCD
  120. },
  121. //Data Declaration
  122. {
  123. { ATT_BT_UUID_SIZE, characterUUID },
  124. GATT_PERMIT_READ,
  125. 0,
  126. &ota_DataProps
  127. },
  128. // Command Value
  129. {
  130. { ATT_UUID_SIZE, ota_DataUUID },
  131. GATT_PERMIT_WRITE,
  132. 0,
  133. &ota_DataValue
  134. }
  135. };
  136. static ota_ProfileChangeCB_t ota_AppCBs = NULL;
  137. static void handleConnStatusCB( uint16 connHandle, uint8 changeType );
  138. static uint8 ota_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  139. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
  140. static bStatus_t ota_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  141. uint8 *pValue, uint8 len, uint16 offset );
  142. CONST gattServiceCBs_t ota_ProfileCBs =
  143. {
  144. ota_ReadAttrCB, // Read callback function pointer
  145. ota_WriteAttrCB, // Write callback function pointer
  146. NULL // Authorization callback function pointer
  147. };
  148. static uint8 ota_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  149. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  150. {
  151. bStatus_t status = ATT_ERR_READ_NOT_PERMITTED;
  152. LOG("ReadAttrCB\n");
  153. // If attribute permissions require authorization to read, return error
  154. if ( gattPermitAuthorRead( pAttr->permissions ) )
  155. {
  156. // Insufficient authorization
  157. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  158. }
  159. return ( status );
  160. }
  161. static bStatus_t ota_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  162. uint8 *pValue, uint8 len, uint16 offset )
  163. {
  164. bStatus_t status = SUCCESS;
  165. //uint8 notifyApp = 0xFF;
  166. // If attribute permissions require authorization to write, return error
  167. if ( gattPermitAuthorWrite( pAttr->permissions ) )
  168. {
  169. // Insufficient authorization
  170. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  171. }
  172. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  173. {
  174. // 16-bit UUID
  175. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  176. if(uuid == GATT_CLIENT_CHAR_CFG_UUID)
  177. {
  178. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  179. offset, GATT_CLIENT_CFG_NOTIFY );
  180. if ( status == SUCCESS && ota_AppCBs)
  181. {
  182. uint16 charCfg = BUILD_UINT16( pValue[0], pValue[1] );
  183. LOG("CCCD set: [%d]\n", charCfg);
  184. if(ota_AppCBs){
  185. ota_Evt_t evt;
  186. evt.ev = charCfg == 1 ? OTA_EVT_NOTIF_ENABLE: OTA_EVT_NOTIF_DISABLE;
  187. evt.size = 0;
  188. evt.data = NULL;
  189. ota_AppCBs(&evt);
  190. }
  191. }
  192. }
  193. }
  194. else
  195. {
  196. //LOG("WR:%d\n", pAttr->handle);
  197. // 128-bit UUID Command
  198. if(pAttr->handle == ota_AttrTbl[OTA_COMMAND_HANDLE].handle)
  199. {
  200. if(ota_AppCBs){
  201. ota_Evt_t evt;
  202. evt.ev = OTA_EVT_CONTROL;
  203. evt.size = len;
  204. evt.data = pValue;
  205. ota_AppCBs(&evt);
  206. }
  207. }
  208. // 128-bit UUID Data
  209. if(pAttr->handle == ota_AttrTbl[OTA_DATA_HANDLE].handle)
  210. {
  211. if(ota_AppCBs){
  212. ota_Evt_t evt;
  213. evt.ev = OTA_EVT_DATA;
  214. evt.size = len;
  215. evt.data = pValue;
  216. ota_AppCBs(&evt);
  217. }
  218. }
  219. }
  220. return ( status );
  221. }
  222. bStatus_t ota_AddService( ota_ProfileChangeCB_t cb)
  223. {
  224. uint8 status = SUCCESS;
  225. // Register with Link DB to receive link status change callback
  226. VOID linkDB_Register( handleConnStatusCB );
  227. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, ota_ResponseCCCD );
  228. // Register GATT attribute list and CBs with GATT Server App
  229. status = GATTServApp_RegisterService( ota_AttrTbl,
  230. GATT_NUM_ATTRS( ota_AttrTbl ),
  231. &ota_ProfileCBs );
  232. if(status!=SUCCESS)
  233. LOG("Add OTA service failed!\n");
  234. ota_AppCBs = cb;
  235. return ( status );
  236. }
  237. static void handleConnStatusCB( uint16 connHandle, uint8 changeType )
  238. {
  239. // Make sure this is not loopback connection
  240. if ( connHandle != LOOPBACK_CONNHANDLE )
  241. {
  242. // Reset Client Char Config if connection has dropped
  243. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  244. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  245. ( !linkDB_Up( connHandle ) ) ) )
  246. {
  247. GATTServApp_InitCharCfg( connHandle, ota_ResponseCCCD);
  248. if(ota_AppCBs){
  249. ota_Evt_t evt;
  250. evt.ev = OTA_EVT_DISCONNECTED;
  251. evt.size = 0;
  252. evt.data = NULL;
  253. ota_AppCBs(&evt);
  254. }
  255. }
  256. }
  257. }
  258. bStatus_t ota_Notify(attHandleValueNoti_t *pNoti)
  259. {
  260. uint16 connHandle;
  261. uint16 value;
  262. GAPRole_GetParameter(GAPROLE_CONNHANDLE, &connHandle);
  263. value = GATTServApp_ReadCharCfg( connHandle, ota_ResponseCCCD);
  264. if(connHandle == INVALID_CONNHANDLE)
  265. return bleIncorrectMode;
  266. // If notifications enabled
  267. if ( value & GATT_CLIENT_CFG_NOTIFY )
  268. {
  269. AT_LOG("Notif %x,%x\n", pNoti->value[0], pNoti->value[1]);
  270. // Set the handle
  271. pNoti->handle = ota_AttrTbl[OTA_RSP_HANDLE].handle;
  272. // Send the Indication
  273. return GATT_Notification( connHandle, pNoti, FALSE);
  274. }
  275. return bleIncorrectMode;
  276. }