ota_app_service.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 <stdio.h>
  30. #include <string.h>
  31. #include "OSAL.h"
  32. #include "linkdb.h"
  33. #include "att.h"
  34. #include "gatt.h"
  35. #include "gatt_uuid.h"
  36. #include "gatt_profile_uuid.h"
  37. #include "peripheral.h"
  38. #include "gattservapp.h"
  39. #include "ota_app_service.h"
  40. #include "log.h"
  41. #include "error.h"
  42. CONST uint8 ota_ServiceUUID[ATT_UUID_SIZE] =
  43. { 0x23, 0xf1, 0x6e, 0x53, 0xa4, 0x22, 0x42, 0x61, 0x91, 0x51, 0x8b, 0x9b, 0x01, 0xff, 0x33, 0x58};
  44. //command characteristic
  45. CONST uint8 ota_CommandUUID[ATT_UUID_SIZE] =
  46. { 0x23, 0xf1, 0x6e, 0x53, 0xa4, 0x22, 0x42, 0x61, 0x91, 0x51, 0x8b, 0x9b, 0x02, 0xff, 0x33, 0x58};
  47. // Response characteristic
  48. CONST uint8 ota_ResponseUUID[ATT_UUID_SIZE] =
  49. { 0x23, 0xf1, 0x6e, 0x53, 0xa4, 0x22, 0x42, 0x61, 0x91, 0x51, 0x8b, 0x9b, 0x03, 0xff, 0x33, 0x58};
  50. static CONST gattAttrType_t ota_Service = { ATT_UUID_SIZE, ota_ServiceUUID };
  51. static uint8 ota_CommandProps = GATT_PROP_WRITE;
  52. static uint8 ota_CommandValue = 0;
  53. // OTA response Characteristic
  54. static uint8 ota_ResponseProps = GATT_PROP_NOTIFY;
  55. static uint8 ota_ResponseValue = 0;
  56. static gattCharCfg_t ota_ResponseCCCD[GATT_MAX_NUM_CONN];
  57. #define OTA_COMMAND_HANDLE 2
  58. #define OTA_RSP_HANDLE 4
  59. #define OTA_DATA_HANDLE 7
  60. static gattAttribute_t ota_AttrTbl[] =
  61. {
  62. //OTA Service
  63. {
  64. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  65. GATT_PERMIT_READ, /* permissions */
  66. 0, /* handle */
  67. (uint8 *)&ota_Service /* pValue */
  68. },
  69. //OTA Command Declaration
  70. {
  71. { ATT_BT_UUID_SIZE, characterUUID },
  72. GATT_PERMIT_READ,
  73. 0,
  74. &ota_CommandProps
  75. },
  76. //OTA Command Value
  77. {
  78. { ATT_UUID_SIZE, ota_CommandUUID },
  79. GATT_PERMIT_WRITE,
  80. 0,
  81. &ota_CommandValue
  82. },
  83. // OTA response Declaration
  84. {
  85. { ATT_BT_UUID_SIZE, characterUUID },
  86. GATT_PERMIT_READ,
  87. 0,
  88. &ota_ResponseProps
  89. },
  90. //response Value
  91. {
  92. { ATT_UUID_SIZE, ota_ResponseUUID },
  93. GATT_PERMIT_READ,
  94. 0,
  95. &ota_ResponseValue
  96. },
  97. // OTA response Client Characteristic Configuration
  98. {
  99. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  100. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  101. 0,
  102. (uint8 *) ota_ResponseCCCD
  103. },
  104. };
  105. typedef struct {
  106. bool notify_en;
  107. uint8_t ver_major;
  108. uint8_t ver_minor;
  109. uint8_t ver_revision;
  110. uint8_t ver_test_build;
  111. uint8_t ota_mode;
  112. uint8_t bank_info;
  113. }ota_app_ctx;
  114. ota_app_ctx s_ota_app;
  115. bool s_reboot_flg = false;
  116. static uint8 ota_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  117. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
  118. static bStatus_t ota_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  119. uint8 *pValue, uint8 len, uint16 offset );
  120. static bStatus_t sendNotify(attHandleValueNoti_t *pNoti);
  121. CONST gattServiceCBs_t ota_ProfileCBs =
  122. {
  123. ota_ReadAttrCB, // Read callback function pointer
  124. ota_WriteAttrCB, // Write callback function pointer
  125. NULL // Authorization callback function pointer
  126. };
  127. /*response format:*/
  128. /*Byte value*/
  129. /*0 error code*/
  130. /*1~19 response data payload*/
  131. static void response(uint8_t* rsp_data, uint8_t size)
  132. {
  133. attHandleValueNoti_t notif;
  134. osal_memset(&notif, 0, sizeof(notif));
  135. if(size > 20)
  136. return;
  137. notif.len = size;
  138. osal_memcpy(notif.value, rsp_data, size);
  139. sendNotify(&notif);
  140. }
  141. static int set_ota_mode(uint8_t mode)
  142. {
  143. if(mode > OTA_MODE_RESOURCE && mode != OTA_MODE_OTA_NADDR)
  144. return PPlus_ERR_INVALID_PARAM;
  145. write_reg(OTA_MODE_SELECT_REG, mode);
  146. return PPlus_SUCCESS;
  147. }
  148. static void load_ota_version(void)
  149. {
  150. uint32_t reg = read_reg(OTA_MODE_SELECT_REG);
  151. s_ota_app.ver_major = (uint8_t)((reg >>4) & 0xf);
  152. s_ota_app.ver_minor = (uint8_t)((reg >>8) & 0xf);
  153. s_ota_app.ver_revision = (uint8_t)((reg >>12) & 0xff);
  154. s_ota_app.ver_test_build = (uint8_t)((reg >>20) & 0xf);
  155. if(s_ota_app.ver_test_build)
  156. LOG("OTA Boot Version: %d.%d.%d%c\n",
  157. s_ota_app.ver_major,s_ota_app.ver_minor,s_ota_app.ver_revision, s_ota_app.ver_test_build+'a'-1);
  158. else
  159. LOG("OTA Boot Version: %d.%d.%d\n",
  160. s_ota_app.ver_major,s_ota_app.ver_minor,s_ota_app.ver_revision);
  161. }
  162. void __attribute__((weak)) ui_firmware_upgrade(void);
  163. static void process_cmd(uint8_t* cmdbuf, uint8_t size){
  164. uint8_t rsp = PPlus_SUCCESS;
  165. switch(cmdbuf[0]){
  166. case OTAAPP_CMD_START_OTA:
  167. {
  168. uint8_t ota_mode = cmdbuf[1];
  169. s_reboot_flg = false;
  170. //set AON register then reboot
  171. rsp = set_ota_mode(ota_mode);
  172. if(rsp == PPlus_SUCCESS){
  173. //GAPRole_TerminateConnection();
  174. if(size != 3){
  175. NVIC_SystemReset();
  176. }
  177. if(cmdbuf[2] != 1){
  178. NVIC_SystemReset();
  179. }
  180. //if cmdbuf[2] is 1
  181. //case host will initiate termination request
  182. //when device terminated, device reboot to ota mode
  183. response(&rsp, 1);
  184. s_reboot_flg = true;
  185. }
  186. else
  187. {
  188. response(&rsp, 1);
  189. }
  190. }
  191. break;
  192. case OTAAPP_CMD_FORMAT:
  193. {
  194. rsp = PPlus_ERR_NOT_SUPPORTED;
  195. //format fs and application
  196. response(&rsp, 1);
  197. }
  198. break;
  199. case OTAAPP_CMD_INFO:
  200. {
  201. //take mac address and version info
  202. uint8_t info_rsp[20];
  203. osal_memset(info_rsp, 0, 20);
  204. info_rsp[0] = PPlus_SUCCESS;
  205. GAPRole_GetParameter(GAPROLE_BD_ADDR, info_rsp+1);
  206. if(s_ota_app.ver_test_build){
  207. sprintf((char*)(info_rsp+7), "V%d.%d.%d%c",
  208. s_ota_app.ver_major,
  209. s_ota_app.ver_minor,
  210. s_ota_app.ver_revision,
  211. 'a'+s_ota_app.ver_test_build-1);
  212. }
  213. else
  214. {
  215. sprintf((char*)(info_rsp+7), "V%d.%d.%d",s_ota_app.ver_major,s_ota_app.ver_minor,s_ota_app.ver_revision);
  216. }
  217. response(info_rsp, 8+strlen((const char*)info_rsp+7));
  218. }
  219. break;
  220. default:
  221. rsp = PPlus_ERR_OTA_UNKNOW_CMD;
  222. response(&rsp, 1);
  223. }
  224. }
  225. static void handleConnStatusCB( uint16 connHandle, uint8 changeType )
  226. {
  227. // Make sure this is not loopback connection
  228. LOG("handleConnStatusCB %x, %d\n", connHandle,changeType );
  229. if ( connHandle != LOOPBACK_CONNHANDLE )
  230. {
  231. // Reset Client Char Config if connection has dropped
  232. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  233. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  234. ( !linkDB_Up( connHandle ) ) ) )
  235. {
  236. GATTServApp_InitCharCfg( connHandle, ota_ResponseCCCD);
  237. if(s_reboot_flg){
  238. NVIC_SystemReset();
  239. }
  240. }
  241. else
  242. {
  243. s_reboot_flg = false;
  244. }
  245. }
  246. }
  247. static bStatus_t sendNotify(attHandleValueNoti_t *pNoti)
  248. {
  249. uint16 connHandle;
  250. uint16 value;
  251. GAPRole_GetParameter(GAPROLE_CONNHANDLE, &connHandle);
  252. value = GATTServApp_ReadCharCfg( connHandle, ota_ResponseCCCD);
  253. if(connHandle == INVALID_CONNHANDLE)
  254. return bleIncorrectMode;
  255. // If notifications enabled
  256. if ( value & GATT_CLIENT_CFG_NOTIFY )
  257. {
  258. // Set the handle
  259. pNoti->handle = ota_AttrTbl[OTA_RSP_HANDLE].handle;
  260. // Send the Indication
  261. return GATT_Notification( connHandle, pNoti, FALSE);
  262. }
  263. return bleIncorrectMode;
  264. }
  265. static uint8 ota_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  266. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  267. {
  268. bStatus_t status = ATT_ERR_READ_NOT_PERMITTED;
  269. LOG("ReadAttrCB\n");
  270. // If attribute permissions require authorization to read, return error
  271. if ( gattPermitAuthorRead( pAttr->permissions ) )
  272. {
  273. // Insufficient authorization
  274. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  275. }
  276. return ( status );
  277. }
  278. static bStatus_t ota_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  279. uint8 *pValue, uint8 len, uint16 offset )
  280. {
  281. bStatus_t status = SUCCESS;
  282. //uint8 notifyApp = 0xFF;
  283. // If attribute permissions require authorization to write, return error
  284. if ( gattPermitAuthorWrite( pAttr->permissions ) )
  285. {
  286. // Insufficient authorization
  287. return ( ATT_ERR_INSUFFICIENT_AUTHOR );
  288. }
  289. if ( pAttr->type.len == ATT_BT_UUID_SIZE )
  290. {
  291. // 16-bit UUID
  292. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  293. if(uuid == GATT_CLIENT_CHAR_CFG_UUID)
  294. {
  295. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  296. offset, GATT_CLIENT_CFG_NOTIFY );
  297. if ( status == SUCCESS)
  298. {
  299. uint16 charCfg = BUILD_UINT16( pValue[0], pValue[1] );
  300. LOG("CCCD set: [%d]\n", charCfg);
  301. s_ota_app.notify_en = (charCfg == 1);
  302. }
  303. }
  304. }
  305. else
  306. {
  307. LOG("WR:%d\n", pAttr->handle);
  308. // 128-bit UUID Command
  309. if(pAttr->handle == ota_AttrTbl[OTA_COMMAND_HANDLE].handle)
  310. {
  311. process_cmd(pValue, len);
  312. }
  313. }
  314. return ( status );
  315. }
  316. bStatus_t ota_app_AddService(void)
  317. {
  318. uint8 status = SUCCESS;
  319. // Register with Link DB to receive link status change callback
  320. VOID linkDB_Register( handleConnStatusCB );
  321. load_ota_version();
  322. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, ota_ResponseCCCD );
  323. // Register GATT attribute list and CBs with GATT Server App
  324. status = GATTServApp_RegisterService( ota_AttrTbl,
  325. GATT_NUM_ATTRS( ota_AttrTbl ),
  326. &ota_ProfileCBs );
  327. if(status!=SUCCESS)
  328. LOG("Add OTA service failed!\n");
  329. return ( status );
  330. }
  331. int ota_vendor_module_StartOTA(uint8_t mode)
  332. {
  333. int ret = 0;
  334. uint32_t reg = read_reg(OTA_MODE_SELECT_REG);
  335. if(reg == 0)
  336. return PPlus_ERR_NOT_REGISTED;
  337. ret = set_ota_mode(mode);
  338. if(ret == PPlus_SUCCESS){
  339. NVIC_SystemReset();
  340. }
  341. return ret;
  342. }
  343. int ota_vendor_module_Version( uint8_t* major, uint8_t* minor, uint8_t* revision, uint8_t *test_build)
  344. {
  345. uint32_t reg = read_reg(OTA_MODE_SELECT_REG);
  346. if(reg == 0)
  347. return PPlus_ERR_NOT_REGISTED;
  348. *major = (uint8_t)((reg >>4) & 0xf);
  349. *minor = (uint8_t)((reg >>8) & 0xf);
  350. *revision = (uint8_t)((reg >>12) & 0xff);
  351. *test_build = (uint8_t)((reg >>20) & 0xf);
  352. return PPlus_SUCCESS;
  353. }