Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_pal_linux_extensions.c Source File

arm_uc_pal_linux_extensions.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "arm_uc_config.h"
00020 #if defined(ARM_UC_FEATURE_PAL_FILESYSTEM) && (ARM_UC_FEATURE_PAL_FILESYSTEM == 1)
00021 #if defined(TARGET_IS_PC_LINUX)
00022 
00023 #include "update-client-pal-filesystem/arm_uc_pal_extensions.h"
00024 
00025 #include "update-client-metadata-header/arm_uc_metadata_header_v2.h"
00026 #include "arm_uc_pal_filesystem_utils.h"
00027 
00028 #include "pal.h"
00029 
00030 #include "mbed-trace/mbed_trace.h"
00031 #define TRACE_GROUP "update-client-extensions"
00032 
00033 #include <stdlib.h>
00034 #include <inttypes.h>
00035 #include <limits.h>
00036 #include <stdio.h>
00037 #include <unistd.h>
00038 
00039 #ifndef MBED_CONF_UPDATE_CLIENT_APPLICATION_DETAILS
00040 #define MBED_CONF_UPDATE_CLIENT_APPLICATION_DETAILS 0
00041 #endif
00042 
00043 #ifndef MBED_CONF_UPDATE_CLIENT_BOOTLOADER_DETAILS
00044 #define MBED_CONF_UPDATE_CLIENT_BOOTLOADER_DETAILS 0
00045 #endif
00046 
00047 static void (*arm_ucex_linux_callback)(uintptr_t) = NULL;
00048 static palImageId_t arm_ucex_activate_image_id;
00049 
00050 #ifndef PAL_UPDATE_ACTIVATE_SCRIPT
00051 #define PAL_UPDATE_ACTIVATE_SCRIPT "./activate_script"
00052 #endif
00053 
00054 arm_uc_error_t pal_ext_imageInitAPI(void (*callback)(uintptr_t))
00055 {
00056     arm_uc_error_t result = { .code = ERR_NONE };
00057 
00058     arm_ucex_linux_callback = callback;
00059 
00060     return result;
00061 }
00062 
00063 arm_uc_error_t pal_ext_imageGetActiveDetails(arm_uc_firmware_details_t *details)
00064 {
00065     arm_uc_error_t result = { .code = ERR_INVALID_PARAMETER };
00066 
00067     if (details) {
00068         /* dummy implementation, return 0 */
00069         memset(details, 0, sizeof(arm_uc_firmware_details_t));
00070 
00071         result.code = ERR_NONE;
00072 
00073         if (arm_ucex_linux_callback) {
00074             arm_ucex_linux_callback(ARM_UC_PAAL_EVENT_GET_ACTIVE_FIRMWARE_DETAILS_DONE);
00075         }
00076     }
00077 
00078     return result;
00079 }
00080 
00081 arm_uc_error_t pal_ext_installerGetDetails(arm_uc_installer_details_t *details)
00082 {
00083     arm_uc_error_t result = { .code = ERR_INVALID_PARAMETER };
00084 
00085     if (details) {
00086         /* dummy implementation, return 0 */
00087         memset(details, 0, sizeof(arm_uc_installer_details_t));
00088 
00089         result.code = ERR_NONE;
00090 
00091         if (arm_ucex_linux_callback) {
00092             arm_ucex_linux_callback(ARM_UC_PAAL_EVENT_GET_INSTALLER_DETAILS_DONE);
00093         }
00094     }
00095 
00096     return result;
00097 }
00098 
00099 static void pal_ext_imageActivationWorker(const void *location)
00100 {
00101     char cmd_buf[sizeof(PAL_UPDATE_ACTIVATE_SCRIPT) + 1 + PAL_MAX_FILE_AND_FOLDER_LENGTH + 1];
00102     char path_buf[PAL_MAX_FILE_AND_FOLDER_LENGTH];
00103 
00104     arm_uc_error_t result = arm_uc_pal_filesystem_get_path(*(palImageId_t *)location, FIRMWARE_IMAGE_ITEM_DATA,
00105                                                            path_buf, PAL_MAX_FILE_AND_FOLDER_LENGTH);
00106     palStatus_t rc = PAL_ERR_GENERIC_FAILURE ;
00107 
00108     if (result.code == ERR_NONE) {
00109         int err = snprintf(cmd_buf, sizeof(cmd_buf), "%s %s",
00110                            PAL_UPDATE_ACTIVATE_SCRIPT, path_buf);
00111         if (err > 0) {
00112             rc = PAL_SUCCESS;
00113         } else {
00114             tr_err("snprintf failed with err %i", err);
00115             rc = PAL_ERR_GENERIC_FAILURE ;
00116         }
00117     }
00118 
00119 
00120     if (rc == PAL_SUCCESS) {
00121         tr_debug("Activate by executing %s", cmd_buf);
00122         int err = system(cmd_buf);
00123         err = WEXITSTATUS(err);
00124 
00125         if (err != -1) {
00126             tr_debug("Activate completed with %" PRId32, err);
00127             rc = PAL_SUCCESS;
00128         } else {
00129             tr_err("system call failed with err %" PRId32, err);
00130             rc = PAL_ERR_GENERIC_FAILURE ;
00131         }
00132     }
00133     fflush(stdout);
00134     sleep(1);
00135 
00136     if (arm_ucex_linux_callback) {
00137         uint32_t event = (rc == PAL_SUCCESS ? ARM_UC_PAAL_EVENT_ACTIVATE_DONE : ARM_UC_PAAL_EVENT_ACTIVATE_ERROR);
00138         arm_ucex_linux_callback(event);
00139     }
00140 }
00141 
00142 arm_uc_error_t pal_ext_imageActivate(uint32_t location)
00143 {
00144     arm_uc_error_t err = { .code = ERR_INVALID_PARAMETER };
00145 
00146     memcpy(&arm_ucex_activate_image_id, &location, sizeof(palImageId_t));
00147 
00148     palThreadID_t thread_id = 0;
00149     palStatus_t rc = pal_osThreadCreateWithAlloc(pal_ext_imageActivationWorker, &arm_ucex_activate_image_id,
00150                                                  PAL_osPriorityBelowNormal, PTHREAD_STACK_MIN, NULL, &thread_id);
00151     if (rc != PAL_SUCCESS) {
00152         tr_err("Thread creation failed with %x", rc);
00153         err.code = ERR_INVALID_PARAMETER;
00154     } else {
00155         tr_debug("Activation thread created, thread ID: %" PRIuPTR, thread_id);
00156         err.code = ERR_NONE;
00157     }
00158 
00159     return err;
00160 }
00161 
00162 #endif /* TARGET_IS_PC_LINUX */
00163 #endif /* ARM_UC_FEATURE_PAL_FILESYSTEM */