Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: NNN50_CE_Test_UDP NNN50_linux_firmware NNN50_SoftAP_HelloWorld NNN50_BLEWIFISensor ... more
m2m_ota.c
00001 /** 00002 * 00003 * \file 00004 * 00005 * \brief NMC1500 IoT OTA Interface. 00006 * 00007 * Copyright (c) 2016-2017 Atmel Corporation. All rights reserved. 00008 * 00009 * \asf_license_start 00010 * 00011 * \page License 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. The name of Atmel may not be used to endorse or promote products derived 00024 * from this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00027 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00028 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00029 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 00030 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00031 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00032 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00033 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00034 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00035 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * \asf_license_stop 00039 * 00040 */ 00041 00042 00043 00044 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 00045 INCLUDES 00046 *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ 00047 #include "common/include/nm_common.h" 00048 #include "driver/include/m2m_types.h" 00049 #include "driver/include/m2m_ota.h" 00050 #include "driver/source/m2m_hif.h" 00051 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 00052 MACROS 00053 *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ 00054 00055 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 00056 DATA TYPES 00057 *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ 00058 static tpfOtaUpdateCb gpfOtaUpdateCb = NULL; 00059 static tpfOtaNotifCb gpfOtaNotifCb = NULL; 00060 00061 00062 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* 00063 FUNCTION PROTOTYPES 00064 *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ 00065 /** 00066 * @fn m2m_wifi_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr, uint8 grp) 00067 * @brief WiFi call back function 00068 * @param [in] u8OpCode 00069 * HIF Opcode type. 00070 * @param [in] u16DataSize 00071 * HIF data length. 00072 * @param [in] u32Addr 00073 * HIF address. 00074 * @param [in] grp 00075 * HIF group type. 00076 * @author 00077 * @date 00078 * @version 1.0 00079 */ 00080 static void m2m_ota_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr) 00081 { 00082 sint8 ret = M2M_SUCCESS; 00083 if(u8OpCode == M2M_OTA_RESP_NOTIF_UPDATE_INFO) 00084 { 00085 tstrOtaUpdateInfo strOtaUpdateInfo; 00086 m2m_memset((uint8*)&strOtaUpdateInfo,0,sizeof(tstrOtaUpdateInfo)); 00087 ret = hif_receive(u32Addr,(uint8*)&strOtaUpdateInfo,sizeof(tstrOtaUpdateInfo),0); 00088 if(ret == M2M_SUCCESS) 00089 { 00090 if(gpfOtaNotifCb) 00091 gpfOtaNotifCb(&strOtaUpdateInfo); 00092 } 00093 } 00094 else if (u8OpCode == M2M_OTA_RESP_UPDATE_STATUS) 00095 { 00096 tstrOtaUpdateStatusResp strOtaUpdateStatusResp; 00097 m2m_memset((uint8*)&strOtaUpdateStatusResp,0,sizeof(tstrOtaUpdateStatusResp)); 00098 ret = hif_receive(u32Addr, (uint8*) &strOtaUpdateStatusResp,sizeof(tstrOtaUpdateStatusResp), 0); 00099 if(ret == M2M_SUCCESS) 00100 { 00101 if(gpfOtaUpdateCb) 00102 gpfOtaUpdateCb(strOtaUpdateStatusResp.u8OtaUpdateStatusType,strOtaUpdateStatusResp.u8OtaUpdateStatus); 00103 } 00104 } 00105 else 00106 { 00107 M2M_ERR("Invaild OTA resp %d ?\n",u8OpCode); 00108 } 00109 00110 } 00111 /*! 00112 @fn \ 00113 NMI_API sint8 m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb, tpfOtaNotifCb pfOtaNotifCb); 00114 00115 @brief 00116 Initialize the OTA layer. 00117 00118 @param [in] pfOtaUpdateCb 00119 OTA Update callback function 00120 00121 @param [in] pfOtaNotifCb 00122 OTA notify callback function 00123 00124 @return 00125 The function SHALL return 0 for success and a negative value otherwise. 00126 */ 00127 NMI_API sint8 m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb, tpfOtaNotifCb pfOtaNotifCb) 00128 { 00129 sint8 ret = M2M_SUCCESS; 00130 00131 if(pfOtaUpdateCb){ 00132 gpfOtaUpdateCb = pfOtaUpdateCb; 00133 }else{ 00134 M2M_ERR("Invaild Ota update cb\n"); 00135 } 00136 if(pfOtaNotifCb){ 00137 gpfOtaNotifCb = pfOtaNotifCb; 00138 }else{ 00139 M2M_ERR("Invaild Ota notify cb\n"); 00140 } 00141 00142 hif_register_cb(M2M_REQ_GROUP_OTA,m2m_ota_cb); 00143 00144 return ret; 00145 } 00146 /*! 00147 @fn \ 00148 NMI_API sint8 m2m_ota_notif_set_url(uint8 * u8Url); 00149 00150 @brief 00151 Set the OTA url 00152 00153 @param [in] u8Url 00154 The url server address 00155 00156 @return 00157 The function SHALL return 0 for success and a negative value otherwise. 00158 */ 00159 NMI_API sint8 m2m_ota_notif_set_url(uint8 * u8Url) 00160 { 00161 sint8 ret = M2M_SUCCESS; 00162 uint16 u16UrlSize = m2m_strlen(u8Url) + 1; 00163 /*Todo: we may change it to data pkt but we need to give it higer priority 00164 but the priorty is not implemnted yet in data pkt 00165 */ 00166 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_NOTIF_SET_URL,u8Url,u16UrlSize,NULL,0,0); 00167 return ret; 00168 00169 } 00170 00171 /*! 00172 @fn \ 00173 NMI_API sint8 m2m_ota_notif_check_for_update(void); 00174 00175 @brief 00176 check for ota update 00177 00178 @return 00179 The function SHALL return 0 for success and a negative value otherwise. 00180 */ 00181 NMI_API sint8 m2m_ota_notif_check_for_update(void) 00182 { 00183 sint8 ret = M2M_SUCCESS; 00184 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_NOTIF_CHECK_FOR_UPDATE,NULL,0,NULL,0,0); 00185 return ret; 00186 } 00187 00188 /*! 00189 @fn \ 00190 NMI_API sint8 m2m_ota_notif_sched(uint32 u32Period); 00191 00192 @brief 00193 Schedule OTA update 00194 00195 @param [in] u32Period 00196 Period in days 00197 00198 @return 00199 The function SHALL return 0 for success and a negative value otherwise. 00200 */ 00201 NMI_API sint8 m2m_ota_notif_sched(uint32 u32Period) 00202 { 00203 sint8 ret = M2M_SUCCESS; 00204 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_NOTIF_CHECK_FOR_UPDATE,NULL,0,NULL,0,0); 00205 return ret; 00206 } 00207 00208 /*! 00209 @fn \ 00210 NMI_API sint8 m2m_ota_start_update(uint8 * u8DownloadUrl); 00211 00212 @brief 00213 Request OTA start update using the downloaded url 00214 00215 @param [in] u8DownloadUrl 00216 The download firmware url, you get it from device info 00217 00218 @return 00219 The function SHALL return 0 for success and a negative value otherwise. 00220 00221 */ 00222 NMI_API sint8 m2m_ota_start_update(uint8 * u8DownloadUrl) 00223 { 00224 sint8 ret = M2M_SUCCESS; 00225 uint16 u16DurlSize = m2m_strlen(u8DownloadUrl) + 1; 00226 /*Todo: we may change it to data pkt but we need to give it higer priority 00227 but the priorty is not implemnted yet in data pkt 00228 */ 00229 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_START_FW_UPDATE,u8DownloadUrl,u16DurlSize,NULL,0,0); 00230 return ret; 00231 } 00232 /*! 00233 @fn \ 00234 NMI_API sint8 m2m_ota_start_update_crt(uint8 * u8DownloadUrl); 00235 00236 @brief 00237 Request OTA start for the Cortus app image. 00238 00239 @param [in] u8DownloadUrl 00240 The cortus application image url. 00241 00242 @return 00243 The function SHALL return 0 for success and a negative value otherwise. 00244 00245 */ 00246 NMI_API sint8 m2m_ota_start_update_crt(uint8 * u8DownloadUrl) 00247 { 00248 sint8 ret = M2M_SUCCESS; 00249 uint16 u16DurlSize = m2m_strlen(u8DownloadUrl) + 1; 00250 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_START_CRT_UPDATE,u8DownloadUrl,u16DurlSize,NULL,0,0); 00251 return ret; 00252 } 00253 00254 00255 /*! 00256 @fn \ 00257 NMI_API sint8 m2m_ota_rollback(void); 00258 00259 @brief 00260 Request OTA Rollback image 00261 00262 @return 00263 The function SHALL return 0 for success and a negative value otherwise. 00264 */ 00265 NMI_API sint8 m2m_ota_rollback(void) 00266 { 00267 sint8 ret = M2M_SUCCESS; 00268 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_ROLLBACK_FW,NULL,0,NULL,0,0); 00269 return ret; 00270 } 00271 /*! 00272 @fn \ 00273 NMI_API sint8 m2m_ota_rollback_crt(void); 00274 00275 @brief 00276 Request Cortus application OTA Rollback image 00277 00278 @return 00279 The function SHALL return 0 for success and a negative value otherwise. 00280 */ 00281 NMI_API sint8 m2m_ota_rollback_crt(void) 00282 { 00283 sint8 ret = M2M_SUCCESS; 00284 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_ROLLBACK_CRT,NULL,0,NULL,0,0); 00285 return ret; 00286 } 00287 00288 /*! 00289 @fn \ 00290 NMI_API sint8 m2m_ota_abort(void); 00291 00292 @brief 00293 Request OTA Abort 00294 00295 @return 00296 The function SHALL return 0 for success and a negative value otherwise. 00297 */ 00298 NMI_API sint8 m2m_ota_abort(void) 00299 { 00300 sint8 ret = M2M_SUCCESS; 00301 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_ABORT,NULL,0,NULL,0,0); 00302 return ret; 00303 } 00304 00305 00306 /*! 00307 @fn \ 00308 NMI_API sint8 m2m_ota_switch_firmware(void); 00309 00310 @brief 00311 Switch to the upgraded Firmware 00312 00313 @return 00314 The function SHALL return 0 for success and a negative value otherwise. 00315 */ 00316 NMI_API sint8 m2m_ota_switch_firmware(void) 00317 { 00318 sint8 ret = M2M_SUCCESS; 00319 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_SWITCH_FIRMWARE,NULL,0,NULL,0,0); 00320 return ret; 00321 } 00322 /*! 00323 @fn \ 00324 NMI_API sint8 m2m_ota_switch_crt(void); 00325 00326 @brief 00327 Switch to the upgraded cortus application. 00328 00329 @return 00330 The function SHALL return 0 for success and a negative value otherwise. 00331 */ 00332 NMI_API sint8 m2m_ota_switch_crt(void) 00333 { 00334 sint8 ret = M2M_SUCCESS; 00335 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_SWITCH_CRT_IMG,NULL,0,NULL,0,0); 00336 return ret; 00337 } 00338 00339 /*! 00340 @fn \ 00341 NMI_API sint8 m2m_ota_get_firmware_version(tstrM2mRev * pstrRev); 00342 00343 @brief 00344 Get the OTA Firmware version. 00345 00346 @return 00347 The function SHALL return 0 for success and a negative value otherwise. 00348 */ 00349 NMI_API sint8 m2m_ota_get_firmware_version(tstrM2mRev * pstrRev) 00350 { 00351 sint8 ret = M2M_SUCCESS; 00352 ret = hif_chip_wake(); 00353 if(ret == M2M_SUCCESS) 00354 { 00355 ret = nm_get_ota_firmware_info(pstrRev); 00356 hif_chip_sleep(); 00357 } 00358 return ret; 00359 } 00360 #if 0 00361 #define M2M_OTA_FILE "../../../m2m_ota.dat" 00362 NMI_API sint8 m2m_ota_test(void) 00363 { 00364 uint32 page = 0; 00365 uint8 buffer[1500]; 00366 uint32 u32Sz = 0; 00367 sint8 ret = M2M_SUCCESS; 00368 FILE *fp =NULL; 00369 fp = fopen(M2M_OTA_FILE,"rb"); 00370 if(fp) 00371 { 00372 fseek(fp, 0L, SEEK_END); 00373 u32Sz = ftell(fp); 00374 fseek(fp, 0L, SEEK_SET); 00375 00376 while(u32Sz > 0) 00377 { 00378 { 00379 page = (rand()%1400); 00380 00381 if((page<100)||(page>1400)) page = 1400; 00382 } 00383 00384 if(u32Sz>page) 00385 { 00386 u32Sz-=page; 00387 } 00388 else 00389 { 00390 page = u32Sz; 00391 u32Sz = 0; 00392 } 00393 printf("page %d\n", (int)page); 00394 fread(buffer,page,1,fp); 00395 ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_TEST|M2M_REQ_DATA_PKT,NULL,0,(uint8*)&buffer,page,0); 00396 if(ret != M2M_SUCCESS) 00397 { 00398 M2M_ERR("\n"); 00399 } 00400 nm_bsp_sleep(1); 00401 } 00402 00403 } 00404 else 00405 { 00406 M2M_ERR("nO err\n"); 00407 } 00408 return ret; 00409 } 00410 #endif 00411 00412
Generated on Wed Jul 13 2022 16:32:37 by
1.7.2