Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OTA feature #256

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions Ameba_misc/Ameba_tools/postbuild_tool/postbuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ string isp_file_name_buf[100];
string nn_model_yolotiny_name, nn_model_srcfd_name, nn_model_mobilefacenet_name, nn_model_yamnet_name, nn_model_imgclass_name, nn_header_name1, nn_header_name2, nn_header_name3, nn_header_name4, nn_header_name5, isp_bin_check_name, ota_mode_check_name, fcs_mode_check_name;
string ino_name_buf[100];

string partitiontable_json_name, partition_bin_name;

int isp_selection_check = 0;
int nn_model_selection_check = 0;
int ota_mode_selection_check = 0;
Expand Down Expand Up @@ -307,7 +309,7 @@ int main(int argc, char *argv[]) {
string string_temp_1 = "cp misc/video_img/";
string string_temp_2 = " ./";
string string_temp_3 = "cp misc/nn_img/";
string string_temp_4 = "cp ";
string string_temp_4 = "cp -r ";
string string_temp_5 = "/*";
string string_temp_6 = "cp image_tool/";
string string_temp_7 = "misc/video_img/";
Expand Down Expand Up @@ -580,21 +582,32 @@ int main(int argc, char *argv[]) {
system(cmd.c_str());
}

ota_mode_check_name = argv[8];
ota_mode_check(ota_mode_check_name);

if (ota_mode_selection_check == 1){
partitiontable_json_name = "amebapro2_partitiontable_OTA.json";
partition_bin_name = "partition_ota.bin";
} else {
partitiontable_json_name = "amebapro2_partitiontable.json";
partition_bin_name = "partition.bin";
}

cmdss.clear();
cmdss << string_temp_1 << "combine amebapro2_partitiontable.json system_files.bin PT_PT=partition.bin,CER_TBL=certable.bin,KEY_CER1=certificate.bin,PT_BL_PRI=boot.bin,PT_FCSDATA=boot_fcs.bin";
cmdss << string_temp_1 << "combine "<< partitiontable_json_name << " system_files.bin PT_PT=" << partition_bin_name << ",CER_TBL=certable.bin,KEY_CER1=certificate.bin,PT_BL_PRI=boot.bin,PT_FCSDATA=boot_fcs.bin";
getline(cmdss, cmd);
cout << cmd << endl;
system(cmd.c_str());

if (nn_model_selection_check == 1) {
cmdss.clear();
cmdss << string_temp_1 << "combine amebapro2_partitiontable.json flash_ntz.bin PT_PT=partition.bin,CER_TBL=certable.bin,KEY_CER1=certificate.bin,PT_BL_PRI=boot.bin,PT_FW1=firmware.bin,PT_NN_MDL=nn_model.bin,PT_ISP_IQ=firmware_isp_iq.bin,PT_FCSDATA=boot_fcs.bin";
cmdss << string_temp_1 << "combine "<< partitiontable_json_name << " flash_ntz.bin PT_PT=" << partition_bin_name << ",CER_TBL=certable.bin,KEY_CER1=certificate.bin,PT_BL_PRI=boot.bin,PT_FW1=firmware.bin,PT_NN_MDL=nn_model.bin,PT_ISP_IQ=firmware_isp_iq.bin,PT_FCSDATA=boot_fcs.bin";
getline(cmdss, cmd);
cout << cmd << endl;
system(cmd.c_str());
} else {
cmdss.clear();
cmdss << string_temp_1 << "combine amebapro2_partitiontable.json flash_ntz.bin PT_PT=partition.bin,CER_TBL=certable.bin,KEY_CER1=certificate.bin,PT_BL_PRI=boot.bin,PT_FW1=firmware.bin,PT_ISP_IQ=firmware_isp_iq.bin,PT_FCSDATA=boot_fcs.bin";
cmdss << string_temp_1 << "combine "<< partitiontable_json_name << " flash_ntz.bin PT_PT=" << partition_bin_name << ",CER_TBL=certable.bin,KEY_CER1=certificate.bin,PT_BL_PRI=boot.bin,PT_FW1=firmware.bin,PT_ISP_IQ=firmware_isp_iq.bin,PT_FCSDATA=boot_fcs.bin";
getline(cmdss, cmd);
cout << cmd << endl;
system(cmd.c_str());
Expand Down Expand Up @@ -623,9 +636,6 @@ int main(int argc, char *argv[]) {
#error compiler is not supported!
#endif

ota_mode_check_name = argv[8];
ota_mode_check(ota_mode_check_name);

if (ota_mode_selection_check == 1) {
cmdss.clear();
cmdss << string_temp_1 << " -E copy "<< string_temp_3 << "firmware.bin "<< string_temp_3 << "ota.bin";
Expand Down
6 changes: 6 additions & 0 deletions Ameba_misc/Ameba_tools/prebuild_tool/prebuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ int main(int argc, char *argv[]) {

// runtime tool path
chdir(argv[1]);

if (strcmp(argv[10], "Enable") == 0 && strcmp(argv[11], "LoadFromFlash") == 0) {
cerr << "OTA is enabled, please select load model from SD card and not load model from flash." << endl;
exit(EXIT_FAILURE);
}

chdir("../..");

isp_camera_option = argv[7];
Expand Down
Binary file not shown.
Binary file modified Arduino_package/ameba_pro2_tools_linux/postbuild_linux
100644 → 100755
Binary file not shown.
Binary file modified Arduino_package/ameba_pro2_tools_linux/prebuild_linux
100644 → 100755
Binary file not shown.
Binary file not shown.
Binary file modified Arduino_package/ameba_pro2_tools_macos/postbuild_macos
100644 → 100755
Binary file not shown.
Binary file modified Arduino_package/ameba_pro2_tools_macos/prebuild_macos
100644 → 100755
Binary file not shown.
Binary file not shown.
Binary file modified Arduino_package/ameba_pro2_tools_windows/postbuild_windows.exe
100644 → 100755
Binary file not shown.
Binary file modified Arduino_package/ameba_pro2_tools_windows/prebuild_windows.exe
100644 → 100755
Binary file not shown.
28 changes: 28 additions & 0 deletions Arduino_package/hardware/libraries/OTA/examples/OTA/OTA.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "ota_thread.h"
#include "WiFi.h"


char ssid[] = "Network_SSID"; // your network SSID (name)
char pass[] = "Password"; // your network password
int status = WL_IDLE_STATUS;

void setup()
{
Serial.begin(115200);

// Connection to internet
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(2000);
}

// Set up the threads
start_OTA_threads();
}

void loop()
{
// Empty or add non-blocking code here
}
18 changes: 18 additions & 0 deletions Arduino_package/hardware/libraries/OTA/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#######################################
# Syntax Coloring Map for OTA
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################


#######################################
# Methods and Functions (KEYWORD2)
#######################################
start_OTA_threads KEYWORD2


#######################################
# Constants (LITERAL1)
#######################################
9 changes: 9 additions & 0 deletions Arduino_package/hardware/libraries/OTA/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=AmebaOTA
version=1.0.0
author=Realtek SG
maintainer=Realtek SG
sentence=Ameba OTA API
paragraph=With this you can do over-the-air firmware updates
category=OTA
url=
architectures=AmebaPro2
47 changes: 47 additions & 0 deletions Arduino_package/hardware/libraries/OTA/src/ota_drv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <osdep_service.h>
#include <wifi_constants.h>
#include "wifi_conf.h"
#include "lwip_netconf.h"
#include <ota_8735b.h>
#include "ota_drv.h"

const int PORT = 3000; // MODIFY THIS FOR YOUR HTTP SERVER PORT
const char *server = "192.168.3.4"; // MODIFY THIS FOR YOUR HTTP SERVER IP ADDRESS
const char *resource = "api/uploadfile"; // DO NOT MODIFY


// DO NOT MODIFY
const char *OtaState[] = {
"OTA_STATE_IDLE",
"OTA_STATE_RECEIVED_START_SIGNAL",
"OTA_STATE_DOWNLOAD_FIRMWARE_IN_PROGRESS",
"OTA_STATE_DOWNLOAD_FIRMWARE_COMPLETED",
"OTA_STATE_REBOOT"};

void http_update_ota_task(void *param)
{
(void)param;

int ret = -1;

g_otaState = OtaState[2];

ret = http_update_ota((char *)server, PORT, (char *)resource);

g_otaState = OtaState[3];

// printf("\n\r[%s] Update task exit", __FUNCTION__);
if (!ret) {
// printf("\n\r[%s] Ready to reboot", __FUNCTION__);
g_otaState = OtaState[4];
ota_platform_reset();
}
vTaskDelete(NULL);
}

void ota_http(void)
{
if (xTaskCreate(http_update_ota_task, (const char *)"http_update_ota_task", 1024, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS) {
printf("\n\r[%s] Create update task failed", __FUNCTION__);
}
}
10 changes: 10 additions & 0 deletions Arduino_package/hardware/libraries/OTA/src/ota_drv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

extern const int PORT;
extern const char *server;
extern const char *resource;

extern const char *OtaState[];
extern const char *g_otaState;

void ota_http(void);
104 changes: 104 additions & 0 deletions Arduino_package/hardware/libraries/OTA/src/ota_thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#define ARDUINOJSON_STRING_LENGTH_SIZE_4

#include <Arduino.h>
#include "ota_thread.h"
#include <WiFi.h>
#include <ArduinoJson.h>

extern "C" {
#include "ota_drv.h"
}

const char *g_otaState = OtaState[0];
JsonDocument doc;
String jsonString;
char buffer[1024];
uint32_t thread1_id, thread2_id, thread3_id, stack_size1, stack_size2;
int priority1;


void sendPostRequest()
{
doc["OTA_state"] = g_otaState;
serializeJson(doc, jsonString);
WiFiClient wifiClient;

if (wifiClient.connect(server, PORT)) {
// Send POST request
wifiClient.println("POST /api/connectedclients HTTP/1.1");
wifiClient.println("Host: " + String(server));
wifiClient.println("Content-Type: application/json"); // Use appropriate content type
wifiClient.println("Content-Length: " + String(jsonString.length())); // Specify the length of the content
wifiClient.println("Connection: keep-alive");
wifiClient.println(); // Empty line indicates the end of headers
wifiClient.print(jsonString);
} else {
Serial.println("Connection to server failed");
}
delay(500);
wifiClient.stop();
delay(3000);
}

void thread1_task(const void *argument)
{
while (1) {
sendPostRequest();
}
}

void thread2_task(const void *argument)
{
WiFiServer server(5000);
server.begin();
while (1) {
WiFiClient client = server.available();

while (client.connected()) {
memset(buffer, 0, 1024);
int n = client.read((uint8_t *)(&buffer[0]), sizeof(buffer));
if (n > 0) {
for (int i = 0; i < n; i++) {
// Serial.print(buffer[i]);
}
n = client.write(buffer, n);
if (n <= 0) {
break;
}
if (strstr(buffer, "start_ota")) {
// Serial.print("\n[OTA] Received start OTA signal from UI.");
if (g_otaState == OtaState[0]) {
ota_http();
}
}
}
delay(500);
client.stop();
}
}
}


void start_OTA_threads()
{
priority1 = osPriorityNormal;
stack_size1 = 1024;
thread1_id = os_thread_create_arduino(thread1_task, NULL, priority1, stack_size1);

// First thread is to do keep alive connectivity check (post requests every 5s)
if (thread1_id) {
Serial.println("[OTA] Keep-alive connectivity thread created successfully.");
} else {
Serial.println("[OTA] Failed to create keep-alive connectivity thread.");
}

stack_size2 = 2048;
thread2_id = os_thread_create_arduino(thread2_task, NULL, priority1, stack_size2);

// Second thread is to get the signal to start OTA process.
if (thread2_id) {
Serial.println("[OTA] Start OTA process thread created successfully.");
} else {
Serial.println("[OTA] Failed to create Start OTA process thread.");
}
}
13 changes: 13 additions & 0 deletions Arduino_package/hardware/libraries/OTA/src/ota_thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef OTA_THREAD_H
#define OTA_THREAD_H

#include <Arduino.h>
#include <httpd/httpd.h>

// Function declarations
void start_OTA_threads();
void thread1_task(const void *argument);
void thread2_task(const void *argument);


#endif // OTA_THREAD_H
6 changes: 3 additions & 3 deletions Arduino_package/hardware/platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ recipe.hooks.prebuild.1.pattern.linux = chmod -R 777 "{ameba.tools_path}"
recipe.hooks.prebuild.1.pattern.macosx= chmod -R 777 "{ameba.tools_path}"

## combine toolchain
recipe.hooks.prebuild.2.pattern.windows = "{ameba.tools_path}/prebuild_windows.exe" "{toolchain.path}" "{toolchain_p1_name_path}" "{toolchain_p2_name_path}" {toolchain_p1_name} {toolchain_p2_name} "{hardware.path}" "{build.camera_opt}" "{nn_tool_name_path}" "{nn_tool_name}"
recipe.hooks.prebuild.2.pattern.linux = {ameba.tools_path}/prebuild_linux {toolchain.path} {toolchain_p1_name_path} {toolchain_p2_name_path} {toolchain_p1_name} {toolchain_p2_name} {hardware.path} {build.camera_opt} {nn_tool_name_path} {nn_tool_name}
recipe.hooks.prebuild.2.pattern.macosx = {ameba.tools_path}/prebuild_macos {toolchain.path} {toolchain_p1_name_path} {toolchain_p2_name_path} {toolchain_p1_name} {toolchain_p2_name} {hardware.path} {build.camera_opt} {nn_tool_name_path} {nn_tool_name}
recipe.hooks.prebuild.2.pattern.windows = "{ameba.tools_path}/prebuild_windows.exe" "{toolchain.path}" "{toolchain_p1_name_path}" "{toolchain_p2_name_path}" {toolchain_p1_name} {toolchain_p2_name} "{hardware.path}" "{build.camera_opt}" "{nn_tool_name_path}" "{nn_tool_name}" "{build.ota_mode_val}" "{build.model_src}"
recipe.hooks.prebuild.2.pattern.linux = {ameba.tools_path}/prebuild_linux {toolchain.path} {toolchain_p1_name_path} {toolchain_p2_name_path} {toolchain_p1_name} {toolchain_p2_name} {hardware.path} {build.camera_opt} {nn_tool_name_path} {nn_tool_name} {build.ota_mode_val} {build.model_src}
recipe.hooks.prebuild.2.pattern.macosx = {ameba.tools_path}/prebuild_macos {toolchain.path} {toolchain_p1_name_path} {toolchain_p2_name_path} {toolchain_p1_name} {toolchain_p2_name} {hardware.path} {build.camera_opt} {nn_tool_name_path} {nn_tool_name} {build.ota_mode_val} {build.model_src}

recipe.hooks.prebuild.3.pattern.windows = "{ameba.tools_path}/ino_validation_windows.exe" "{build.path}" "{ameba.tools_path}" "{build.model_src}"
recipe.hooks.prebuild.3.pattern.linux = {ameba.tools_path}/ino_validation_linux {build.path} {ameba.tools_path} {build.model_src}
Expand Down
27 changes: 27 additions & 0 deletions Arduino_package/package_realtek_amebapro2_early_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,33 @@
"size":"77770301"
}
]
},
{
"name": "ameba_pro2_tools",
"version": "1.3.7",
"systems": [
{
"host": "i686-mingw32",
"archiveFileName": "ameba_pro2_tools_windows-1.3.7.tar.gz",
"url": "https://github.com/ambiot/ambpro2_arduino/raw/dev/Arduino_package/release/ameba_pro2_tools_windows-1.3.7.tar.gz",
"checksum": "SHA-256:220740c069c61fc94db9e086889d4a1b1aee2c2f5d82fb275daef11806c8eec5",
"size": "9426215"
},
{
"host": "x86_64-pc-linux-gnu",
"archiveFileName": "ameba_pro2_tools_linux-1.3.7.tar.gz",
"url": "https://github.com/ambiot/ambpro2_arduino/raw/dev/Arduino_package/release/ameba_pro2_tools_linux-1.3.7.tar.gz",
"checksum": "SHA-256:985f00a1532d5be0a3250a183e92456565c8628a746962b74188d65c7b8e7def",
"size": "13271103"
},
{
"host": "x86_64-apple-darwin",
"archiveFileName": "ameba_pro2_tools_macos-1.3.7.tar.gz",
"url": "https://github.com/ambiot/ambpro2_arduino/raw/dev/Arduino_package/release/ameba_pro2_tools_macos-1.3.7.tar.gz",
"checksum": "SHA-256:75696955e08537908c0ad4637e4c5454cb5c6de23ac611fc4fc69a8b64829cfa",
"size": "11471387"
}
]
},
{
"name": "ameba_pro2_tools",
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.