diff --git a/001_OH_KP_Thread/BUILD.gn b/001_OH_KP_Thread/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..30a40169ca0c4b91817c4935c6817d73fe7338f2 --- /dev/null +++ b/001_OH_KP_Thread/BUILD.gn @@ -0,0 +1,6 @@ +static_library("thread_sample") { + sources = [ + "thread_sample.c", + ] + +} diff --git a/001_OH_KP_Thread/BUILD.gn.bak b/001_OH_KP_Thread/BUILD.gn.bak new file mode 100644 index 0000000000000000000000000000000000000000..a92df9eb5ce1e11570f245d417bf0f91391af7d2 --- /dev/null +++ b/001_OH_KP_Thread/BUILD.gn.bak @@ -0,0 +1,19 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("task_test") { + sources = [ + "task_test.c", + ] + +} diff --git a/001_OH_KP_Thread/thread_sample.c b/001_OH_KP_Thread/thread_sample.c new file mode 100644 index 0000000000000000000000000000000000000000..f680e663746d1821219ea22368304a4f3355f727 --- /dev/null +++ b/001_OH_KP_Thread/thread_sample.c @@ -0,0 +1,83 @@ +#include +//#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" + + +void thread1(void) +{ + int sum = 0; + while (1) + { + printf("This is thread1----%d\r\n", sum++); + usleep(1000000); + } +} + + +void thread2(void) +{ + int sum = 0; + while (1) + { + printf("This is thread2----%d\r\n", sum++); + usleep(500000); + } +} + + +static void Thread_sample(void) +{ + osThreadAttr_t attr; + + attr.name = "thread1"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 1024 * 4; + attr.priority = 25; + + osThreadId_t threadID1 = osThreadNew((osThreadFunc_t)thread1, NULL, &attr); + if (threadID1 == NULL) + { + printf("Falied to create thread1!\n"); + } + + attr.name = "thread2"; + + osThreadId_t threadID2 = osThreadNew((osThreadFunc_t)thread2, NULL, &attr); + if (threadID2 == NULL) + { + printf("Falied to create thread2!\n"); + } + + int sum = 0; + usleep(1000000); + osStatus_t status = osThreadSuspend(threadID1); + printf("This is main thread: suspend thread1, result:%d\r\n", status); + + for (int i = 0; i < 5; i++) + { + printf("This is main thread----%d\r\n", sum++); + usleep(1000000); + } + + status = osThreadResume(threadID1); + printf("This is main thread: resume thread1, result:%d\r\n", status); + + usleep(1000000); + status = osThreadTerminate(threadID1); + printf("This is main thread: terminate thread1, result:%d\r\n", status); + status = osThreadTerminate(threadID2); + printf("This is main thread: terminate thread2, result:%d\r\n", status); + + while (1) + { + printf("This is main thread----%d\r\n", sum++); + usleep(1000000); + } +} + +APP_FEATURE_INIT(Thread_sample); diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0c82df64dbb585c18cdbbc9d229c4883fcb3a60e --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,11 @@ +import("//build/lite/config/component/lite_component.gni") + +lite_component("app") { + features = [ + #"GPS:gps", + #"EBox_IoT_028_Env_GPIO_LED:gpio_out_led", + "001_OH_KP_Thread:thread_sample", + # "002_OH_KP_Event:Event_sample", + # "003_OH_KP_Timer:Timer_sample", + ] +} diff --git a/EBox_IoT_028_Env_GPIO_LED/BUILD.gn b/EBox_IoT_028_Env_GPIO_LED/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e950f9739872689c1c45a9cd0cbcf2af605627dc --- /dev/null +++ b/EBox_IoT_028_Env_GPIO_LED/BUILD.gn @@ -0,0 +1,10 @@ +static_library("gpio_out_led") { + sources = [ + "gpio_out_led.c", + ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "../../iot_hardware_hals/include", + ] +} \ No newline at end of file diff --git a/EBox_IoT_028_Env_GPIO_LED/gpio_out_led.c b/EBox_IoT_028_Env_GPIO_LED/gpio_out_led.c new file mode 100644 index 0000000000000000000000000000000000000000..c4b1662f1b481a0337128fb123f650c68731e0be --- /dev/null +++ b/EBox_IoT_028_Env_GPIO_LED/gpio_out_led.c @@ -0,0 +1,121 @@ +#include +// #include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "los_task.h" +//#include "los_debug.h" +#include "iot_gpio_ex.h" +#include "iot_gpio.h" +#define LED_OFF 0 +#define LED_ON 1 +#define LED_SPARK 2 +#define LED_NAME_GPIO_5 5 +#define LED_SPARK_INTERVAL_TIME 50 +int g_ledState; + + +void LedGpioInit(void) +// 初始化LED引脚 +{ + int ret = IoTGpioInit(LED_NAME_GPIO_5); + if (ret != 0) + { + printf("IoTGpioInit failed :%#x \r\n", ret); + return; + } + /*设置复用为GPIO功能*/ + ret = IoTGpioSetFunc(LED_NAME_GPIO_5, IOT_GPIO_FUNC_GPIO_5_GPIO); + if (ret != 0) + { + printf("IoTGpioSetFunc failed :%#x \r\n", ret); + return; + } + /*设置方向为输出*/ + ret = IoTGpioSetDir(LED_NAME_GPIO_5, IOT_GPIO_DIR_OUT); + if (ret != 0) + { + printf("IoTGpioSetDir failed :%#x \r\n", ret); + return; + } + printf("----- LedGpioInit success! -----\r\n"); +} +void LedGpioCtrl(unsigned short ledSta) +{ + switch (ledSta) + { + + case LED_ON: + { + IoTGpioSetOutputVal(LED_NAME_GPIO_5, LED_ON); + break; + } + case LED_OFF: + { + IoTGpioSetOutputVal(LED_NAME_GPIO_5, LED_OFF); + break; + } + case LED_SPARK: + { + IoTGpioSetOutputVal(LED_NAME_GPIO_5, LED_ON); + osDelay(LED_SPARK_INTERVAL_TIME); + IoTGpioSetOutputVal(LED_NAME_GPIO_5, LED_OFF); + osDelay(LED_SPARK_INTERVAL_TIME); + IoTGpioSetOutputVal(LED_NAME_GPIO_5, LED_ON); + osDelay(LED_SPARK_INTERVAL_TIME); + IoTGpioSetOutputVal(LED_NAME_GPIO_5, LED_OFF); + break; + } + default: + break; + } +} +static void GpiooutTask(void) +{ + /*循环检测按键状态并处理*/ + while (1) + { + switch (g_ledState) + { + case LED_ON: + g_ledState = LED_SPARK; + printf("led State : SPART \r\n"); + break; + case LED_OFF: + g_ledState = LED_ON; + printf("led State : ON \r\n"); + break; + case LED_SPARK: + g_ledState = LED_OFF; + printf("led State : OFF \r\n"); + break; + default: + g_ledState = LED_OFF; + break; + } + LedGpioCtrl(g_ledState); + osDelay(LED_SPARK_INTERVAL_TIME); + } + return; +} +void GpiooutSamp1e(void) +{ + // 初始化 + LedGpioInit(); + /*设置线程属性*/ + osThreadAttr_t attr; + attr.name = "GpioutTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 1024 * 4; + attr.priority = 20; + /*创建定时控制LED灯线程*/ + osThreadId_t threadID1 = osThreadNew((osThreadFunc_t)GpiooutTask, NULL, &attr); + if (threadID1 = NULL) + { + printf("Falied to create GpiooutTask!\r\n"); + } +} +APP_FEATURE_INIT(GpiooutSamp1e); \ No newline at end of file diff --git a/GPS/BUILD.gn b/GPS/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c0a2e3ecee9d73c87367fc48c17847e003c21602 --- /dev/null +++ b/GPS/BUILD.gn @@ -0,0 +1,10 @@ +static_library("gps") { + sources = [ + "gps.c", + ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "../../iot_hardware_hals/include", + ] +} \ No newline at end of file diff --git a/GPS/gps.c b/GPS/gps.c new file mode 100644 index 0000000000000000000000000000000000000000..5b1141ca959391a3e5633b85b67f5693b921c338 --- /dev/null +++ b/GPS/gps.c @@ -0,0 +1,75 @@ +#include +#include +#include + +#include "cmsis_os2.h" +#include "iot_errno.h" +#include "iot_uart.h" +#include "ohos_init.h" + +#define UART_TASK_STACK_SIZE (1024 * 8) +#define UART_TASK_PRIO 25 +#define UART_BUFF_SIZE 1000 +#define UART1 1 +#define TASK_DELAY_1S 1000000 + +static const char *data = "uart_task\r\n"; + +/** + * @brief uart task send data through uart1 and receive data through uart1 + * + */ +static void UartTask(void) +{ + uint8_t uart_buff[UART_BUFF_SIZE] = {1,2}; + uint8_t *uart_buff_ptr = uart_buff; + uint8_t ret; + + IotUartAttribute uart_attr = { + .baudRate = 9600, + .dataBits = 8, + .stopBits = 1, + .parity = 0, + }; + + // Initialize uart driver + ret = IoTUartInit(UART1, &uart_attr); + if (ret != IOT_SUCCESS) { + printf("Failed to init uart! Err code = %d\n", ret); + return; + } + + while (1) { + printf("=======================================\r\n"); + printf("*************UART_example**************\r\n"); + printf("=======================================\r\n"); + + // send data through uart1 + IoTUartWrite(UART1, (unsigned char *)data, strlen(data)); + + // receive data through uart1 + IoTUartRead(UART1, uart_buff_ptr, UART_BUFF_SIZE); + + printf("Uart1 read data:%s\n", uart_buff_ptr); + usleep(TASK_DELAY_1S); + } +} + +static void UartExampleEntry(void) +{ + osThreadAttr_t attr; + + attr.name = "UartTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = UART_TASK_STACK_SIZE; + attr.priority = UART_TASK_PRIO; + + if (osThreadNew((osThreadFunc_t)UartTask, NULL, &attr) == NULL) { + printf("Failed to create UartTask!\n"); + } +} + +APP_FEATURE_INIT(UartExampleEntry); \ No newline at end of file diff --git a/common/communicationkit/js_api/@system.communicationkit.d.ts b/common/communicationkit/js_api/@system.communicationkit.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..4805ec267f65e4d201a3b0e36788d80b64558c09 --- /dev/null +++ b/common/communicationkit/js_api/@system.communicationkit.d.ts @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + */ +export interface GetCommunicationKitOptions { + /** + * Content index. + * For liteWearable and smartVision, the value contains a maximum of 32 characters and cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + key: string; + + /** + * Default value returned when the key does not exist. + * If this parameter is not specified, an empty string is returned. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + default?: string; + + /** + * Called when the stored content is read successfully. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + success?: (data: any) => void; + + /** + * Called when the stored content fails to be read. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + fail?: (data: string, code: number) => void; + + /** + * Called when the execution is completed. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + complete?: () => void; +} + +/** + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + */ +export interface SetCommunicationKitOptions { + /** + * Index of the stored content to be modified. + * For liteWearable and smartVision, the value contains a maximum of 32 characters and cannot contain special characters such as \/"*+,:;<=>?[]|\x7F. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + key: string; + + /** + * Target storage content. If the content is an empty string, the data item with the key as the index will be deleted. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + value: string; + + /** + * Called when the stored content is modified successfully. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + success?: () => void; + + /** + * Called when the stored content fails to be modified. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + fail?: (data: string, code: number) => void; + + /** + * Called when the execution is completed. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + * @since 3 + */ + complete?: () => void; +} + + + + + +/** + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + */ +export default class CommunicationKit { + /** + * Reads the stored content. + * @param options Options. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + */ + static get(options: GetCommunicationKitOptions): void; + + /** + * Modifies the stored content. + * @param options Options. + * @devices tv, phone, tablet, wearable, liteWearable, smartVision + */ + static set(options: SetCommunicationKitOptions): void; +} diff --git a/common/communicationkit/js_api/BUILD.gn b/common/communicationkit/js_api/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..6d9bc116de719f0bd3afd64cd0e790524d56e95a --- /dev/null +++ b/common/communicationkit/js_api/BUILD.gn @@ -0,0 +1,25 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("//build/ohos/notice/notice.gni") +import("//build/templates/metadata/module_info.gni") + +ohos_copy("communicationkit_api") { + sources = [ + "@system.communicationkit.d.ts", + ] + outputs = [ target_out_dir + "/$target_name/{{source_file_part}}" ] + module_source_dir = target_out_dir + "/$target_name" + module_install_name = "" +} \ No newline at end of file diff --git a/common/communicationkit/native_utils/BUILD.gn b/common/communicationkit/native_utils/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..01eaa68b5181164df139327d7deae9b06d9d1c12 --- /dev/null +++ b/common/communicationkit/native_utils/BUILD.gn @@ -0,0 +1,44 @@ +# +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/subsystem/aafwk/path.gni") + +lite_library("native_communication_kit") { + + target_type = "static_library" + + sources = [ + "src/nativeapi_communication_kit.cpp", + "src/nativeapi_communication_kit_impl.c", + ] + cflags = [ "-Wall" ] + cflags_cc = cflags + + include_dirs = [ + "include", + "//utils/native/lite/js/builtin/common/include", + "//third_party/bounds_checking_function/include", + "//foundation/ace/ace_engine_lite/interfaces/innerkits/builtin", + "//foundation/ace/ace_engine_lite/interfaces/innerkits/builtin/base", + "//foundation/ace/ace_engine_lite/interfaces/innerkits/builtin/async", + "${aafwk_lite_path}/interfaces/kits/ability_lite", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + ] + deps = [ + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_static", + "//third_party/bounds_checking_function:libsec_shared", + ] +} diff --git a/common/communicationkit/native_utils/include/nativeapi_communication_kit.h b/common/communicationkit/native_utils/include/nativeapi_communication_kit.h new file mode 100644 index 0000000000000000000000000000000000000000..627e9dd807175c75a7a4e9af9e76d3fa0f83d6a2 --- /dev/null +++ b/common/communicationkit/native_utils/include/nativeapi_communication_kit.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JS_NATIVE_API_COMMUNICATION_KIT_H +#define JS_NATIVE_API_COMMUNICATION_KIT_H + +#include "jsi/jsi.h" + +namespace OHOS { +namespace ACELite { +class NativeapiCommunicationKit { +public: + NativeapiCommunicationKit() = default; + ~NativeapiCommunicationKit() = default; + + static JSIValue Get(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum); + static JSIValue Set(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum); + +}; +} // ACELite +} // OHOS +#endif // JS_NATIVE_API_COMMUNICATION_KIT_H \ No newline at end of file diff --git a/common/communicationkit/native_utils/include/nativeapi_communication_kit_impl.h b/common/communicationkit/native_utils/include/nativeapi_communication_kit_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..fda8f3d6864d7f63c0efb0aed7da52ba30f716a8 --- /dev/null +++ b/common/communicationkit/native_utils/include/nativeapi_communication_kit_impl.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JS_FS_COMMUNICATION_KIT_IMPL_API_H +#define JS_FS_COMMUNICATION_KIT_IMPL_API_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +#define KEY_MAX_LEN 32 +#define VALUE_MAX_LEN 128 + + +#define LOG_TAG "COMMUNICATION_KIT" +#include "hilog/hiview_log.h" + +#define LOG_E(fmt, ...) HILOG_ERROR(HILOG_MODULE_APP, fmt, ##__VA_ARGS__) +#define LOG_I(fmt, ...) HILOG_INFO(HILOG_MODULE_APP, fmt, ##__VA_ARGS__) + + +typedef int (*JsCallClangFunctionDef)(const char *key, char *value); + +void JsGetClangFuncationRegister(JsCallClangFunctionDef jsGetClang); +void JsSetClangFuncationRegister(JsCallClangFunctionDef jsSetClang); +void JsGetClangFuncationUnregister(void); +void JsSetClangFuncationUnregister(void); + +int GetCommunicationKitValue(const char* key, char* value); +int SetCommunicationKitValue(const char* key, const char* value); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* JS_FS_COMMUNICATION_KIT_IMPL_API_H */ \ No newline at end of file diff --git a/common/communicationkit/native_utils/src/nativeapi_communication_kit.cpp b/common/communicationkit/native_utils/src/nativeapi_communication_kit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cec06cdc4792ec97f64a0fb435a65c7ec89b556 --- /dev/null +++ b/common/communicationkit/native_utils/src/nativeapi_communication_kit.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nativeapi_communication_kit.h" +#include "nativeapi_communication_kit_impl.h" + +#include +#include "ability_env.h" +#include "js_async_work.h" +#include "nativeapi_common.h" +#include "nativeapi_config.h" + + +namespace OHOS { +namespace ACELite { +namespace { +char g_kvFullPath[FILE_NAME_MAX_LEN + 1] = {0}; + +bool IsValidKey(const char* key) +{ + if (key == nullptr) { + return false; + } + size_t keyLen = strnlen(key, KEY_MAX_LEN + 1); + if ((keyLen == 0) || (keyLen > KEY_MAX_LEN)) { + return false; + } + if (strpbrk(key, "/\\\"*+,:;<=>\?[]|\x7F")) { + return false; + } + return true; +} + +JSIValue ExecuteAsyncWork(const JSIValue thisVal, const JSIValue* args, + uint8_t argsNum, AsyncWorkHandler ExecuteFunc, bool flag) +{ + JSIValue undefValue = JSI::CreateUndefined(); + if (!NativeapiCommon::IsValidJSIValue(args, argsNum)) { + return undefValue; + } + FuncParams* params = new FuncParams(); + if (params == nullptr) { + return undefValue; + } + params->thisVal = JSI::AcquireValue(thisVal); + params->args = JSI::AcquireValue(args[0]); + params->flag = flag; + JsAsyncWork::DispatchAsyncWork(ExecuteFunc, reinterpret_cast(params)); + return undefValue; +} + +void ExecuteGet(void* data) +{ + FuncParams* params = reinterpret_cast(data); + if (params == nullptr) { + return; + } + + JSIValue args = params->args; + JSIValue thisVal = params->thisVal; + char* key = JSI::GetStringProperty(args, KV_KEY); + const char* dataPath = GetDataPath(); + JSIValue result = JSI::CreateUndefined(); + int ret = ERROR_CODE_GENERAL; + char* value = reinterpret_cast(malloc(VALUE_MAX_LEN + 1)); + if (value == nullptr) { + NativeapiCommon::FailCallBack(thisVal, args, ret); + goto EXIT; + } + + ret = GetCommunicationKitValue(key, value); + + if (ret == ERROR_FR_NO_FILE) { + // GetDefault(thisVal, args); + goto EXIT; + } + if (ret != NATIVE_SUCCESS) { + NativeapiCommon::FailCallBack(thisVal, args, ret); + goto EXIT; + } + result = JSI::CreateString(value); + NativeapiCommon::SuccessCallBack(thisVal, args, result); +EXIT: + free(value); + JSI::ReleaseString(key); + JSI::ReleaseValueList(args, thisVal, result, ARGS_END); + delete params; +} + +void ExecuteSet(void* data) +{ + FuncParams* params = reinterpret_cast(data); + if (params == nullptr) { + return; + } + + JSIValue args = params->args; + JSIValue thisVal = params->thisVal; + int ret = ERROR_CODE_GENERAL; + + char* key = JSI::GetStringProperty(args, KV_KEY); + char* value = JSI::GetStringProperty(args, KV_VALUE); + + LOG_I("JsSet:ExecuteSet::key=%s,value=%s\r\n",key,value); + + if ((key == nullptr) || !strlen(key)) { + NativeapiCommon::FailCallBack(thisVal, args, ERROR_CODE_PARAM); + goto EXIT; + } + if ((value == nullptr) || !strlen(value)) { + NativeapiCommon::SuccessCallBack(thisVal, args, JSI::CreateUndefined()); + goto EXIT; + } + ret = SetCommunicationKitValue(key, value); + if (ret != NATIVE_SUCCESS) { + NativeapiCommon::FailCallBack(thisVal, args, ret); + goto EXIT; + } + NativeapiCommon::SuccessCallBack(thisVal, args, JSI::CreateUndefined()); + +EXIT: + JSI::ReleaseString(key); + JSI::ReleaseString(value); + JSI::ReleaseValueList(args, thisVal, ARGS_END); + delete params; +} + +} + +void InitNativeApiCommunicationKit(JSIValue exports) +{ + JSI::SetModuleAPI(exports, "get", NativeapiCommunicationKit::Get); + JSI::SetModuleAPI(exports, "set", NativeapiCommunicationKit::Set); +} + +JSIValue NativeapiCommunicationKit::Get(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum) +{ + return ExecuteAsyncWork(thisVal, args, argsNum, ExecuteGet, false); +} + +JSIValue NativeapiCommunicationKit::Set(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum) +{ + return ExecuteAsyncWork(thisVal, args, argsNum, ExecuteSet, false); +} + +} // ACELite +} // OHOS diff --git a/common/communicationkit/native_utils/src/nativeapi_communication_kit_impl.c b/common/communicationkit/native_utils/src/nativeapi_communication_kit_impl.c new file mode 100644 index 0000000000000000000000000000000000000000..57684bc7619701a373e130e952d2168712888058 --- /dev/null +++ b/common/communicationkit/native_utils/src/nativeapi_communication_kit_impl.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nativeapi_communication_kit_impl.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "nativeapi_config.h" +#if (defined _WIN32 || defined _WIN64) +#include "shlwapi.h" +#endif + +static JsCallClangFunctionDef g_jsGetClang = NULL; +static JsCallClangFunctionDef g_jsSetClang = NULL; + +static bool IsValidValue(const char *value) +{ + if (value == NULL) + { + return false; + } + size_t valueLen = strnlen(value, VALUE_MAX_LEN + 1); + if ((valueLen == 0) || (valueLen > VALUE_MAX_LEN)) + { + return false; + } + return true; +} + +int GetCommunicationKitValue(const char *key, char *value) +{ + if ((key == NULL) || (value == NULL)) + { + return ERROR_CODE_PARAM; + } + if (NULL != g_jsGetClang) + { + if (0 != g_jsGetClang(key, value)) + { + LOG_E("g_jsGetClang error!\r\n"); + } + } + else + { + LOG_I("If you want use js get c interface, you should use JsGetClangFuncationRegister function.\r\n"); + } + return NATIVE_SUCCESS; +} + +int SetCommunicationKitValue(const char *key, const char *value) +{ + if ((key == NULL) || (!IsValidValue(value))) + { + return ERROR_CODE_PARAM; + } + if (NULL != g_jsSetClang) + { + if (0 != g_jsSetClang(key, value)) + { + LOG_E("g_jsSetClang error!\r\n"); + } + LOG_I("CommunicationKit:SetJsonValue::entry--key=%s;value=%s.\r\n", key, value); + } + else + { + LOG_I("If you want use js set c interface, you should use JsSetClangFuncationRegister function.\r\n"); + } + return NATIVE_SUCCESS; +} + +void JsGetClangFuncationRegister(JsCallClangFunctionDef jsGetClang) +{ + g_jsGetClang = jsGetClang; +} +void JsSetClangFuncationRegister(JsCallClangFunctionDef jsSetClang) +{ + g_jsSetClang = jsSetClang; +} + +void JsGetClangFuncationUnregister(void) +{ + g_jsGetClang = NULL; +} +void JsSetClangFuncationUnregister(void) +{ + g_jsSetClang = NULL; +} \ No newline at end of file diff --git a/common/hals/BUILD.gn b/common/hals/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..51811784e8d07ee4fbcde00f86952d6c47961b76 --- /dev/null +++ b/common/hals/BUILD.gn @@ -0,0 +1,24 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("hals") { + sources = [ + "src/peripheral_hal.c", + "src/utils_hal.c" + ] + + include_dirs = [ + "../inc", + "//base/iot_hardware/peripheral/interfaces/kits", + ] +} \ No newline at end of file diff --git a/common/hals/src/peripheral_hal.c b/common/hals/src/peripheral_hal.c new file mode 100644 index 0000000000000000000000000000000000000000..28488ce9ae10d1f81b01a9515ba238e0fb967d40 --- /dev/null +++ b/common/hals/src/peripheral_hal.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "peripheral_hal.h" +#include "iot_errno.h" +#include "hi_io.h" +#include "hi_adc.h" +#include "hi_types_base.h" +#include "hi_watchdog.h" +#include "hi_pwm.h" + +unsigned int HalIoSetFunc(HalWifiIotIoName id, const char *val) +{ + if (id == HAL_WIFI_IOT_IO_NAME_MAX) { + return IOT_FAILURE; + } + return hi_io_set_func((hi_io_name)id, val); +} + +unsigned int HalAdcRead(HalWifiIotAdcChannelIndex channel, unsigned short *data, HalWifiIotAdcEquModelSel equModel, + HalWifiIotAdcCurBais curBais, unsigned short rstCnt) +{ + return hi_adc_read((hi_adc_channel_index)channel, (hi_u16*)data, (hi_adc_equ_model_sel)equModel, + (hi_adc_cur_bais)curBais, (hi_u16)rstCnt); +} + +void HalSetWatchDogEnable(int enable) +{ + if (enable == 0) { + hi_watchdog_disable(); + } else { + hi_watchdog_enable(); + } +} + +unsigned int HalPwmStart(uint32 id, uint16 duty, uint16 freq) +{ + return hi_pwm_start((hi_pwm_port)id, (hi_u16)duty, (hi_u16)freq); +} \ No newline at end of file diff --git a/common/hals/src/utils_hal.c b/common/hals/src/utils_hal.c new file mode 100644 index 0000000000000000000000000000000000000000..ef34078b38618fb154a9eee05d30d31ab7026cbd --- /dev/null +++ b/common/hals/src/utils_hal.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ohos_types.h" +#include "hi_time.h" +#include "hi_types_base.h" +#include "utils_hal.h" + +void hal_udelay(uint32 us) +{ + hi_udelay((hi_u32)us); +} \ No newline at end of file diff --git a/common/inc/flower.h b/common/inc/flower.h new file mode 100644 index 0000000000000000000000000000000000000000..cc46dbd60695111aa3e3c0ac545270f08698295e --- /dev/null +++ b/common/inc/flower.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __FLOWER_H__ +#define __FLOWER_H__ + + +int FlowerInit(void); + +int FlowerSetMotorStatus(int status); + +int FlowerGetAirStatus(int *temperature, int *humidity); + +int FlowerGetSoilStatus(int *moisture); + +void FlowerDeinit(void); + +#endif // __FLOWER_H__ diff --git a/common/inc/iot_adc.h b/common/inc/iot_adc.h new file mode 100644 index 0000000000000000000000000000000000000000..6a0f383f4f2e816f68654614a95a697119fb4f40 --- /dev/null +++ b/common/inc/iot_adc.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup IotHardware + * @{ + * + * @brief Provides APIs for operating devices, + * including flash, GPIO, I2C, PWM, UART,ADC, and watchdog APIs. + * + * + * + * @since 2.2 + * @version 2.2 + */ + +/** + * @file iot_adc.h + * + * @brief Declares the ADC interface functions for you to read data. + * + * @since 1.0 + * @version 1.1.0 + */ + +#ifndef IOT_ADC_H +#define IOT_ADC_H + + +/** + * @brief Enumerates analog power control modes. + */ +typedef enum { + /** Automatic control */ + IOT_ADC_CUR_BAIS_DEFAULT, + /** Automatic control */ + IOT_ADC_CUR_BAIS_AUTO, + /** Manual control (AVDD = 1.8 V) */ + IOT_ADC_CUR_BAIS_1P8V, + /** Manual control (AVDD = 3.3 V) */ + IOT_ADC_CUR_BAIS_3P3V, + /** Button value */ + IOT_ADC_CUR_BAIS_BUTT, +} IotAdcCurBais; + +/** + * @brief Enumerates equation models. + */ +typedef enum { + /** One-equation model */ + IOT_ADC_EQU_MODEL_1, + /** Two-equation model */ + IOT_ADC_EQU_MODEL_2, + /** Four-equation model */ + IOT_ADC_EQU_MODEL_4, + /** Eight-equation model */ + IOT_ADC_EQU_MODEL_8, + /** Button value */ + IOT_ADC_EQU_MODEL_BUTT, +} IotAdcEquModelSel; + +/** + * @brief Reads a piece of sampled data from a specified ADC channel based on the input parameters. + * + * + * + * @param channel Indicates the ADC channel index. + * @param data Indicates the pointer to the address for storing the read data. + * @param equModel Indicates the equation model. + * @param curBais Indicates the analog power control mode. + * @param rstCnt Indicates the count of the time from reset to conversion start. + * One count is equal to 334 ns. The value must range from 0 to 0xFF0. + * @return Returns {@link IOT_SUCCESS} if the PWM signal output is stopped; + * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTAdcRead(unsigned int channel, unsigned short *data, IotAdcEquModelSel equModel, + IotAdcCurBais curBais, unsigned short rstCnt); + +#endif +/** @} */ \ No newline at end of file diff --git a/common/inc/iot_boardbutton.h b/common/inc/iot_boardbutton.h new file mode 100644 index 0000000000000000000000000000000000000000..5f2485f0b9ba928ac196e84cfd465d3f7dd80cd4 --- /dev/null +++ b/common/inc/iot_boardbutton.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 zhangqf1314@163.com . + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IOT_BOARDBUTTON_H_ +#define IOT_BOARDBUTTON_H_ + +typedef void (*ButtonPressedCallback)(char *arg); + + +/** + * @brief Initialize a button + * + */ +int Board_IsButtonPressedF2(void); +int Board_InitButtonF1(ButtonPressedCallback, char *arg); +int Board_InitButtonF2(ButtonPressedCallback, char *arg); + + +#endif /* IOT_BOARDBUTTON_H_ */ diff --git a/common/inc/iot_boardbutton_xradio.h b/common/inc/iot_boardbutton_xradio.h new file mode 100644 index 0000000000000000000000000000000000000000..a08612c7399eacc02f60aa96ec34c0090b243c69 --- /dev/null +++ b/common/inc/iot_boardbutton_xradio.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 zhangqf1314@163.com . + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IOT_BOARDBUTTON_H_ +#define IOT_BOARDBUTTON_H_ + +typedef enum { + BUTTON_INVALID = 0xff, + BUTTON_RELEASE = 0, + BUTTON_PRESS, + BUTTON_LONG_PRESS, + + BUTTON_MAX_TYPE +} BUTTON_VALUE_TYPE; + +typedef struct { + int button_id; + BUTTON_VALUE_TYPE value; +} ButtonEvent; + +typedef void (*ButtonPressedCallback)(ButtonEvent *event); + +/** + * @brief Initialize a button + * + */ +int Board_ButtonInit(ButtonPressedCallback callback); + +int Board_IsButtonPressed(void); + +#endif /* IOT_BOARDBUTTON_H_ */ diff --git a/common/inc/iot_boardled.h b/common/inc/iot_boardled.h new file mode 100644 index 0000000000000000000000000000000000000000..f0ea44a464e6a9f2aa1ff225168bb5b737126906 --- /dev/null +++ b/common/inc/iot_boardled.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_BOARDLED_H__ +#define __IOT_BOARDLED_H__ + +/** + * @brief Initialize the board pwd led + * @return Returns 0 success while others failed + */ +int BOARD_InitPwmLed(void); + +/** + * @brief Set Pwm led duty cycle + * @param dutyCycle, the duty cycle to set, not max than 40000 + * @return Returns 0 success while others failed + */ +int BOARD_SetPwdLedDutyCycle(int dutyCycle); + + +/** + * @brief Initialize the LED + * @return Returns 0 success while others failed + */ +int BOARD_InitIoLed(void); + +/** + * @brief Control the led status + * @param status Indicates the status to set and value should be CN_BOARD_SWICT_ON/OFF + * @return Returns 0 success while others failed + */ +int BOARD_SetIoLedStatus(int status); + + +/** + * @brief use this function to change the flash cycle; + * @param flashHz indicates the flash frequency + * @return Returns 0 success while others failed + */ + +int LedFlashFrequencySet(float flashHz); + +/** + * @brief use this function to kill the flash task + * @return Returns 0 success while others failed + */ +int LedFlashTaskDeinit(void); + +#endif /* __IOT_BOARDLED_H__ */ diff --git a/common/inc/iot_boardled_xradio.h b/common/inc/iot_boardled_xradio.h new file mode 100644 index 0000000000000000000000000000000000000000..8ae3016be04eeb14f939c6dda8d0757679cc1f6e --- /dev/null +++ b/common/inc/iot_boardled_xradio.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_BOARDLED_H__ +#define __IOT_BOARDLED_H__ + +typedef enum { + LED_FLASH_FAST_PLUGS, + LED_FLASH_FAST, + LED_FLASH_NORMAL, + LED_FLASH_SLOW, + LED_FLASH_SLOW_PLUGS, + + LED_FLASH_MAX +}LED_FLASH_MODE; + +/** + * @brief use this function to change the flash cycle; + * @param flashHz indicates the flash frequency + * @return Returns 0 success while others failed + */ + +int LedFlashFrequencySet(LED_FLASH_MODE mode); + +/** + * @brief use this function to kill the flash task + * @return Returns 0 success while others failed + */ +int LedFlashTaskDeinit(void); + +/** + * @brief Initialize the LED task + */ +void LedFlashTaskStart(void); + +#endif /* __IOT_BOARDLED_H__ */ diff --git a/common/inc/iot_cloud.h b/common/inc/iot_cloud.h new file mode 100644 index 0000000000000000000000000000000000000000..823a9da0595e334007456a3c587326c22ced2d56 --- /dev/null +++ b/common/inc/iot_cloud.h @@ -0,0 +1,107 @@ +#ifndef __IOT_CLOUD_H__ +#define __IOT_CLOUD_H__ + +#include +#include + +/** + * @brief Initialize the cloud sdk + * @return 0 success while others failed + */ +int CLOUD_Init(void); +/** + * @brief Do deinitialize the cloud sdk + * @return 0 success while others failed + */ +int CLOUD_Deinit(void); + +/** + * @brief Send collected data to Cloud Platform + * @param jsonString, which means this has been formated as the profile defines + * @return Returns 0 success while others failed +*/ +int CLOUD_ReportMsg(const char *jsonString); + +/** + * @brief Connect to the Cloud Platform + * @param deviceID Indicats the deviceID create in the iot platform + * @param devicePwd Indicates the corresponding to the deviceID + * @param serverIP Indicates the ip of the iot platform + * @param serverPort Indicates the port correspond to the ip + * @param cmdCallBack Indicates command callback and will be called if any message comes + * @return Returns 0 success while others failed +*/ +int CLOUD_Connect(const char *deviceID, const char *devicePwd, \ + const char *serverIP, const char *serverPort); + +/** + * @brief Disconnect from the Cloud Platform + * @return 0 success while others failed +*/ +int CLOUD_Disconnect(void); + +/** + * @brief use this is a call back function implemented by the demo + * @param jsonString indicated the jsonString received from the iot_cloud + * @return Returns 0 success while -1 failed + */ +int CLOUD_CommandCallBack(const char *jsonString); + + +/** + * @brief functions and data for the syntax format + * + */ + +// enum all the data type for the oc profile +typedef enum { + IOT_PROFILE_KEY_DATATYPE_INT = 0, + IOT_PROFILE_KEY_DATATYPE_LONG, + IOT_PROFILE_KEY_DATATYPE_FLOAT, + IOT_PROFILE_KEY_DATATYPE_DOUBLE, + IOT_PROFILE_KEY_DATATYPE_STRING, + IOT_PROFILE_KEY_DATATYPE_LAST, +}IotProfileDataType; + + +typedef struct { + void *nxt; ///< ponit to the next key + char *key; + IotProfileDataType type; + void *value; +}IotProfileKV; + + +typedef struct { + void *nxt; + char *serviceID; ///< the service id in the profile, which could not be NULL + char *eventTime; ///< eventtime, which could be NULL means use the platform time + IotProfileKV *propertyLst; ///< the property in the profile, which could not be NULL +} IotProfileService; + + +/** + * @brief Package the profile to json string mode, and you should free it manually + * @param serviceLst, profile services + * @return Returns the formates json string or NULL if failed + * + */ +char *IoTProfilePackage(IotProfileService *serviceLst); + +char *IotNotificationPackage(int type, const char *enString, const char *chString); + +typedef enum { + NOTIFY_TYPE_NORMAL = 0, + NOTIFY_TYPE_SECONDARY, + NOTIFY_TYPE_URGENT, + NOTIFY_TYPE_LAST +} NOTIFY_TYPE; + +int CLOUD_ReportNotification(int type, const char *enString, const char *chString); + + + +int CLOUD_GetCloudConnectedStatus(void); + +#endif /* __IOT_CLOUD_H__ */ + diff --git a/common/inc/iot_demo_def.h b/common/inc/iot_demo_def.h new file mode 100644 index 0000000000000000000000000000000000000000..6c85fc945f902b442e23ecf2a0e6ac88e6cc4d2f --- /dev/null +++ b/common/inc/iot_demo_def.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_DEMO_DEF_H__ +#define __IOT_DEMO_DEF_H__ + +#define CN_BOARD_SWITCH_ON 1 +#define CN_BOARD_SWITCH_OFF 0 +#define CN_HOURS_IN_DAY 24 +#define CN_MINUTES_IN_HOUR 60 +#define CN_SECONDS_IN_MINUTE 60 +#define CN_SECONDS_IN_HOUR (CN_MINUTES_IN_HOUR * CN_SECONDS_IN_MINUTE) +#define CN_MINISECONDS_IN_SECOND 1000 +#define CN_MINUTES_IN_DAY (CN_HOURS_IN_DAY * CN_MINUTES_IN_HOUR) +#define CN_SECONS_IN_DAY (CN_SECONDS_IN_MINUTE * CN_MINUTES_IN_DAY) +#define BASE_YEAR_OF_TIME_CALC 1900 + +#define AFTER_NETCFG_ACTION 2 +#define BUFF_SIZE 256 + +typedef enum { + + LOG_LEVEL_INFO = 0, + LOG_LEVEL_DEBUG, + LOG_LEVEL_WARN, + LOG_LEVEL_ERR, + LOG_LEVEL_EXCEPTION, + LOG_LEVEL_EXIT, +}LogLevelType; + +//we defines a log arise function +#define RAISE_LOG 1 + +#ifdef RAISE_LOG +#define RaiseLog(level,fmt, ...) \ + do \ + { \ + printf("[%s][%u][%d] " fmt "\r\n", \ + __FUNCTION__,__LINE__,level, ##__VA_ARGS__); \ + } while (0) +#else +#define RaiseLog(level,fmt, ...) +#endif + + +#endif /* __IOT_DEMO_DEF_H__ */ diff --git a/common/inc/iot_gpio_ex.h b/common/inc/iot_gpio_ex.h new file mode 100644 index 0000000000000000000000000000000000000000..703dcd2aeeb03a88d8bdbfc1c7dafbfa02c8df51 --- /dev/null +++ b/common/inc/iot_gpio_ex.h @@ -0,0 +1,340 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * @file iot_gpio_ex.h + * + * @brief Declares the extended GPIO interface functions. + * + * These functions are used for settings GPIO pulls and driver strength. \n + * + * @since 1.0 + * @version 1.0 + */ + +#ifndef IOT_GPIO_EX_H +#define IOT_GPIO_EX_H + + +/** + * @brief Enumerates the functions of GPIO hardware pin 0. + */ +typedef enum { + /** GPIO0 function */ + IOT_GPIO_FUNC_GPIO_0_GPIO, + /** Functions of UART1 TXD */ + IOT_GPIO_FUNC_GPIO_0_UART1_TXD = 2, + /** SPI1 CK function */ + IOT_GPIO_FUNC_GPIO_0_SPI1_CK, + /** Functions of JTAG TD0 */ + IOT_GPIO_FUNC_GPIO_0_JTAG_TDO, + /** PWM3 OUT function */ + IOT_GPIO_FUNC_GPIO_0_PWM3_OUT, + /** I2C1 SDA function */ + IOT_GPIO_FUNC_GPIO_0_I2C1_SDA, +} WifiIotIoFuncGpio0; + +/** + * @brief Enumerates the functions of GPIO hardware pin 1. + */ +typedef enum { + /** GPIO1 function */ + IOT_GPIO_FUNC_GPIO_1_GPIO, + /** UART1 RXD function */ + IOT_GPIO_FUNC_GPIO_1_UART1_RXD = 2, + /** SPI1 RXD function */ + IOT_GPIO_FUNC_GPIO_1_SPI1_RXD, + /** JTAG TCKfunction */ + IOT_GPIO_FUNC_GPIO_1_JTAG_TCK, + /** PWM4 OUT function */ + IOT_GPIO_FUNC_GPIO_1_PWM4_OUT, + /** I2C1 SCL function */ + IOT_GPIO_FUNC_GPIO_1_I2C1_SCL, + /** BT FREQ function */ + IOT_GPIO_FUNC_GPIO_1_BT_FREQ, +} WifiiIotIoFuncGpio1; + +/** + * @brief Enumerates the functions of GPIO hardware pin 2. + */ +typedef enum { + /** GPIO2 function */ + IOT_GPIO_FUNC_GPIO_2_GPIO, + /** UART1 RTS function */ + IOT_GPIO_FUNC_GPIO_2_UART1_RTS_N = 2, + /** SPI1 TXD function */ + IOT_GPIO_FUNC_GPIO_2_SPI1_TXD, + /** JTAG TRSTN function */ + IOT_GPIO_FUNC_GPIO_2_JTAG_TRSTN, + /** PWM2 OUT function */ + IOT_GPIO_FUNC_GPIO_2_PWM2_OUT, + /** SSI CLK function */ + IOT_GPIO_FUNC_GPIO_2_SSI_CLK = 7, +} WifiIotIoFuncGpio2; + +/** + * @brief Enumerates the functions of GPIO hardware pin 3. + */ +typedef enum { + /** GPIO3 function */ + IOT_GPIO_FUNC_GPIO_3_GPIO, + /** UART0 TXD function */ + IOT_GPIO_FUNC_GPIO_3_UART0_TXD, + /** UART1 CTS function */ + IOT_GPIO_FUNC_GPIO_3_UART1_CTS_N, + /** SPI CSN function */ + IOT_GPIO_FUNC_GPIO_3_SPI1_CSN, + /** JTAG TDI function */ + IOT_GPIO_FUNC_GPIO_3_JTAG_TDI, + /** PWM5 OUT function */ + IOT_GPIO_FUNC_GPIO_3_PWM5_OUT, + /** I2C1 SDA function */ + IOT_GPIO_FUNC_GPIO_3_I2C1_SDA, + /** SSI DATA function */ + IOT_GPIO_FUNC_GPIO_3_SSI_DATA, +} WifiIotIoFuncGpio3; + +/** + * @brief Enumerates the functions of GPIO hardware pin 4. + */ +typedef enum { + /** GPIO4 function */ + IOT_GPIO_FUNC_GPIO_4_GPIO, + /** UART0 RXD function */ + IOT_GPIO_FUNC_GPIO_4_UART0_RXD = 2, + /** JTAG TMS function */ + IOT_GPIO_FUNC_GPIO_4_JTAG_TMS = 4, + /** PWM1 OUT function */ + IOT_GPIO_FUNC_GPIO_4_PWM1_OUT, + /** I2C1 SCL function */ + IOT_GPIO_FUNC_GPIO_4_I2C1_SCL, +} WifiIotIoFuncGpio4; + +/** + * @brief Enumerates the functions of GPIO hardware pin 5. + */ +typedef enum { + /** GPIO5 function */ + IOT_GPIO_FUNC_GPIO_5_GPIO, + /** UART1 RXD function */ + IOT_GPIO_FUNC_GPIO_5_UART1_RXD = 2, + /** SPI0 CSN function */ + IOT_GPIO_FUNC_GPIO_5_SPI0_CSN, + /** PWM2 OUT function */ + IOT_GPIO_FUNC_GPIO_5_PWM2_OUT = 5, + /** I2C0 MCLK function */ + IOT_GPIO_FUNC_GPIO_5_I2S0_MCLK, + /** BT STATUS function */ + IOT_GPIO_FUNC_GPIO_5_BT_STATUS, +} WifiIotIoFuncGpio5; + +/** + * @brief Enumerates the functions of GPIO hardware pin 6. + */ +typedef enum { + /** GPIO6 function */ + IOT_GPIO_FUNC_GPIO_6_GPIO, + /** UART1 TXD function */ + IOT_GPIO_FUNC_GPIO_6_UART1_TXD = 2, + /** SPI0 CK function */ + IOT_GPIO_FUNC_GPIO_6_SPI0_CK, + /** PWM3 OUT function */ + IOT_GPIO_FUNC_GPIO_6_PWM3_OUT = 5, + /** I2S0 TX function */ + IOT_GPIO_FUNC_GPIO_6_I2S0_TX, + /** COEX switch function */ + IOT_GPIO_FUNC_GPIO_6_COEX_SWITCH, +} WifiIotIoFuncGpio6; + +/** + * @brief Enumerates the functions of GPIO hardware pin 7. + */ +typedef enum { + /** GPIO7 function */ + IOT_GPIO_FUNC_GPIO_7_GPIO, + /** UART1 CTS function */ + IOT_GPIO_FUNC_GPIO_7_UART1_CTS_N = 2, + /** SPI0 RXD function */ + IOT_GPIO_FUNC_GPIO_7_SPI0_RXD, + /** PWM0 OUT function */ + IOT_GPIO_FUNC_GPIO_7_PWM0_OUT = 5, + /** I2S0 BCLK function */ + IOT_GPIO_FUNC_GPIO_7_I2S0_BCLK, + /** BT ACTIVE function */ + IOT_GPIO_FUNC_GPIO_7_BT_ACTIVE, +} WifiIotIoFuncGpio7; + +/** + * @brief Enumerates the functions of GPIO hardware pin 8. + */ +typedef enum { + /** GPIO8 function */ + IOT_GPIO_FUNC_GPIO_8_GPIO, + /** UART1 RTS function */ + IOT_GPIO_FUNC_GPIO_8_UART1_RTS_N = 2, + /** SPI0 TXD function */ + IOT_GPIO_FUNC_GPIO_8_SPI0_TXD, + /** PWM1 OUT function */ + IOT_GPIO_FUNC_GPIO_8_PWM1_OUT = 5, + /** I2S0 WS function */ + IOT_GPIO_FUNC_GPIO_8_I2S0_WS, + /** WLAN ACTIVE function */ + IOT_GPIO_FUNC_GPIO_8_WLAN_ACTIVE, +} WifiIotIoFuncGpio8; + +/** + * @brief Enumerates the functions of GPIO hardware pin 9. + */ +typedef enum { + /** GPIO9 function */ + IOT_GPIO_FUNC_GPIO_9_GPIO, + /** I2C0 SCL function */ + IOT_GPIO_FUNC_GPIO_9_I2C0_SCL, + /** UART2 RTS function */ + IOT_GPIO_FUNC_GPIO_9_UART2_RTS_N, + /** SDIO D2 function */ + IOT_GPIO_FUNC_GPIO_9_SDIO_D2, + /** SPI0 TXD function */ + IOT_GPIO_FUNC_GPIO_9_SPI0_TXD, + /** PWM0 OUT function */ + IOT_GPIO_FUNC_GPIO_9_PWM0_OUT, + /** I2S0 MCLK function */ + IOT_GPIO_FUNC_GPIO_9_I2S0_MCLK = 7, +} WifiIotIoFuncGpio9; + +/** + * @brief Enumerates the functions of GPIO hardware pin 10. + */ +typedef enum { + /** GPIO10 function */ + IOT_GPIO_FUNC_GPIO_10_GPIO, + /** I2C0 SDA function */ + IOT_GPIO_FUNC_GPIO_10_I2C0_SDA, + /** UART2 CTS function */ + IOT_GPIO_FUNC_GPIO_10_UART2_CTS_N, + /** SDIO D3 function */ + IOT_GPIO_FUNC_GPIO_10_SDIO_D3, + /** SPI0 CK function */ + IOT_GPIO_FUNC_GPIO_10_SPI0_CK, + /** PWM1 OUT function */ + IOT_GPIO_FUNC_GPIO_10_PWM1_OUT, + /** I2S0 TX function */ + IOT_GPIO_FUNC_GPIO_10_I2S0_TX = 7, +} WifiIotIoFuncGpio10; + +/** + * @brief Enumerates the functions of GPIO hardware pin 11. + */ +typedef enum { + /** GPIO11 function */ + IOT_GPIO_FUNC_GPIO_11_GPIO, + /** UART2 TXD function */ + IOT_GPIO_FUNC_GPIO_11_UART2_TXD = 2, + /** SDIO CMD function */ + IOT_GPIO_FUNC_GPIO_11_SDIO_CMD, + /** SDIO RXD function */ + IOT_GPIO_FUNC_GPIO_11_SPI0_RXD, + /** PWM2 OUT function */ + IOT_GPIO_FUNC_GPIO_11_PWM2_OUT, + /** RF TX_EN_EXT function */ + IOT_GPIO_FUNC_GPIO_11_RF_TX_EN_EXT, + /** I2S0 RX function */ + IOT_GPIO_FUNC_GPIO_11_I2S0_RX, +} WifiIotIoFuncGpio11; + +/** + * @brief Enumerates the functions of GPIO hardware pin 12. + */ +typedef enum { + /** GPIO12 function */ + IOT_GPIO_FUNC_GPIO_12_GPIO, + /** SUART2 RXD function */ + IOT_GPIO_FUNC_GPIO_12_UART2_RXD = 2, + /** SDIO CLK function */ + IOT_GPIO_FUNC_GPIO_12_SDIO_CLK, + /** SDIO CSN function */ + IOT_GPIO_FUNC_GPIO_12_SPI0_CSN, + /** PWM3 OUT function */ + IOT_GPIO_FUNC_GPIO_12_PWM3_OUT, + /** RF RX_EN_EXT function */ + IOT_GPIO_FUNC_GPIO_12_RF_RX_EN_EXT, + /** I2S0 BCLK function */ + IOT_GPIO_FUNC_GPIO_12_I2S0_BCLK, +} WifiIotIoFuncGpio12; + +/** + * @brief Enumerates the functions of GPIO hardware pin 13. + */ +typedef enum { + /** SSI DATA function */ + IOT_GPIO_FUNC_GPIO_13_SSI_DATA, + /** UART0 TXD function */ + IOT_GPIO_FUNC_GPIO_13_UART0_TXD, + /** UART2 RTS function */ + IOT_GPIO_FUNC_GPIO_13_UART2_RTS_N, + /** SDIO D0 function */ + IOT_GPIO_FUNC_GPIO_13_SDIO_D0, + /** GPIO13 function */ + IOT_GPIO_FUNC_GPIO_13_GPIO, + /** PWM4 OUT function */ + IOT_GPIO_FUNC_GPIO_13_PWM4_OUT, + /** I2C0 SDA function */ + IOT_GPIO_FUNC_GPIO_13_I2C0_SDA, + /** I2S0 WS function */ + IOT_GPIO_FUNC_GPIO_13_I2S0_WS, +} WifiIotIoFuncGpio13; + +/** + * @brief Enumerates the functions of GPIO hardware pin 14. + */ +typedef enum { + /** SSI CLK function */ + IOT_GPIO_FUNC_GPIO_14_SSI_CLK, + /** UART0 RXD function */ + IOT_GPIO_FUNC_GPIO_14_UART0_RXD, + /** UART2 CTS function */ + IOT_GPIO_FUNC_GPIO_14_UART2_CTS_N, + /** SDIO D1 function */ + IOT_GPIO_FUNC_GPIO_14_SDIO_D1, + /** GPIO14 function */ + IOT_GPIO_FUNC_GPIO_14_GPIO, + /** PWM5 OUT function */ + IOT_GPIO_FUNC_GPIO_14_PWM5_OUT, + /** I2C0 SCL function */ + IOT_GPIO_FUNC_GPIO_14_I2C0_SCL, +} WifiIotIoFuncGpio14; + +/** + * @brief Enumerates GPIO pull-up or pull-down settings. + */ +typedef enum { + /** No pull */ + IOT_GPIO_PULL_NONE, + /** Pull-up */ + IOT_GPIO_PULL_UP, + /** Pull-down */ + IOT_GPIO_PULL_DOWN, + /** Maximum value */ + IOT_GPIO_PULL_MAX, +} IotGpioPull; + + +unsigned int IoTGpioSetFunc(unsigned int id, unsigned char val); + +unsigned int IoTGpioSetPull(unsigned int id, IotGpioPull val); + +#endif +/** @} */ \ No newline at end of file diff --git a/common/inc/iot_i2c_ex.h b/common/inc/iot_i2c_ex.h new file mode 100644 index 0000000000000000000000000000000000000000..e1f09524d32d09d19e4cc87edf1a3106a25347fc --- /dev/null +++ b/common/inc/iot_i2c_ex.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * @addtogroup wifiiot + * @{ + * + * @brief Provides dedicated device operation interfaces on the Wi-Fi module, + * including ADC, AT, flash, GPIO, I2C, I2S, partition, PWM, SDIO, UART, and watchdog. + * + * + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file wifiiot_i2c_ex.h + * + * @brief Declares the extended I2C interface functions. + * + * These functions are used for I2C baud rate setting and device exception callback. \n + * + * @since 1.0 + * @version 1.0 + */ + +#ifndef IOT_I2C_EX_H +#define IOT_I2C_EX_H + + +/** + * @brief Defines I2C data transmission attributes. + */ +typedef struct { + /** Pointer to the buffer storing data to send */ + unsigned char *sendBuf; + /** Length of data to send */ + unsigned int sendLen; + /** Pointer to the buffer for storing data to receive */ + unsigned char *receiveBuf; + /** Length of data received */ + unsigned int receiveLen; +} IotI2cData; + + +/** + * @brief Sends data to and receives data responses from an I2C device. + * + * + * + * @param id Indicates the I2C device ID. + * @param deviceAddr Indicates the I2C device address. + * @param i2cData Indicates the pointer to the device descriptor of the data to receive. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTI2cWriteread(unsigned int id, unsigned short deviceAddr, const IotI2cData *i2cData); + + + +#endif +/** @} */ diff --git a/common/inc/iot_list.h b/common/inc/iot_list.h new file mode 100644 index 0000000000000000000000000000000000000000..e5bd8f824c692a37646f1033b9c30315cb8c6439 --- /dev/null +++ b/common/inc/iot_list.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_LIST_H__ +#define __IOT_LIST_H__ + +typedef void* IOT_LIST; + +IOT_LIST IoT_ListCreate(void *data, int size); + +int IoT_ListAppend(IOT_LIST mHandle, void *data, int size); + +int IoT_ListUpdate(IOT_LIST mHandle, int idx, void *data, int size); + +int IoT_ListDelete(IOT_LIST mHandle, void *data); + +int IoT_ListGet(IOT_LIST mHandle, int idx, void *data, int size); + +int IoT_ListGetSize(IOT_LIST mHandle); + +void IoT_ListClear(IOT_LIST mHandle); + +void IoT_ListDestroy(IOT_LIST mHandle); + +#endif /* __IOT_LIST_H__ */ diff --git a/common/inc/iot_netcfg.h b/common/inc/iot_netcfg.h new file mode 100644 index 0000000000000000000000000000000000000000..9d1f6445d9fabf5eae884a4a603d1488d1ef9ee6 --- /dev/null +++ b/common/inc/iot_netcfg.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_NETCFG_H__ +#define __IOT_NETCFG_H__ + +/** + * @brief start net config. + * + * @param ap_name: the ap name + * @param ssid: the buff to store the wifi ssid + * @param sLen: the ssid buff size + * @param pwd: the buff to store the wifi password + * @param pLen: the pwd buff size + * + * @since 1.0 + * @version 1.0 + * + * @return 0 success, -1 failed + */ +int BOARD_NetCfgStartConfig(const char *appName, char *ssid, int sLen, char *pwd, int pLen); + +#endif /* __IOT_NETCFG_H__ */ diff --git a/common/inc/iot_netcfg_nan.h b/common/inc/iot_netcfg_nan.h new file mode 100644 index 0000000000000000000000000000000000000000..55f7671f7c517883056adcd2d5a0a1e93de4fc70 --- /dev/null +++ b/common/inc/iot_netcfg_nan.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_NETCFG_NAN_H__ +#define __IOT_NETCFG_NAN_H__ + +#define NETCFG_TASK_STACK_SIZE (1024*4) +#define NETCFG_TASK_PRIO 30 +#define NETCFG_LED_INTERVAL_TIME_US 300000 +#define NETCFG_BIZ_SLEEP_TIME_US 1000000 +#define NETCFG_TIME_COUNT 5 +#define DEVICE_INFO_NUM 2 +#define POWER_NUM (-52) +#define MAX_DATA_LEN 4096 +#define FAST_CONNECT_RETRY_NUM 3 + +#define CHANNEL_80211B_ONLY 14 +#define FREQ_OF_CHANNEL_1 2412 +#define FREQ_OF_CHANNEL_80211B_ONLY 2484 +#define WIFI_MIN_CHANNEL 1 +#define WIFI_FREQ_INTERVAL 5 +#define TEST_CONNECT_RETRY_COUNT 5 + +typedef enum { + NET_EVENT_NULL, + NET_EVENT_CONFIG, + NET_EVENT_CONFIG_FAIL, // Connected wifi failed + NET_EVENT_CONFIG_SUCC, // Connected wifi success + NET_EVENT_CONNECTTED, // Wifi connected + NET_EVENT_DISTCONNECT, // Wifi disconnected + NET_EVENT_RECV_DATA, // Recv message from FA + NET_EVENT_SEND_DATA, // Send the message to FA + + NET_EVENT_TYPE_NBR +}NET_EVENT_TYPE; + +/** + * @brief: the network config service callback + * + * @param event reference {@link NET_EVENT_TYPE} + * @param data The data of the event: NET_EVENT_RECV_DATA or NET_EVENT_SEND_DATA + * @since 1.0 + * @version 1.0 + */ +typedef int (*NetCfgEventCallback)(NET_EVENT_TYPE event, void *data); + +/** + * @brief Register the network config task + * + * @since 1.0 + * @version 1.0 + */ +void NetCfgRegister(void); + +/** + * @brief start net config. + * + * @param ssid: the buff to store the wifi ssid + * @param sLen: the ssid buff size + * @param pwd: the buff to store the wifi password + * @param pLen: the pwd buff size + * + * @since 1.0 + * @version 1.0 + * + * @return 0 success, -1 failed + */ +int BOARD_NAN_NetCfgStartConfig(const char *apName, char *ssid, int sLen, char *pwd, int pLen); +#endif // __IOT_NETCFG_NAN_H__ \ No newline at end of file diff --git a/common/inc/iot_nfc.h b/common/inc/iot_nfc.h new file mode 100644 index 0000000000000000000000000000000000000000..1f9eca7088f9cf382b90145b39b8f126aa03392c --- /dev/null +++ b/common/inc/iot_nfc.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_NFC_H__ +#define __IOT_NFC_H__ + + +/** + * @brief Initialize the board NFC + * @return Returns 0 success while others failed + */ +int BOARD_InitNfc(void); + +/** + * @brief Defines the nfc information + */ +typedef struct { + const char *deviceID; + const char *devicePWD; + const char *wifiSSID; + const char *wifiPWD; +}NfcInfo; + +/** + * @brief Get the nfc info + * @param info Indicates the buffer to storage the data get from NFC + * @return Returns 0 success while others failed + */ +int BOARD_GetNfcInfo(NfcInfo *info); + + +#endif /* __IOT_NFC_H__ */ diff --git a/common/inc/iot_schedule.h b/common/inc/iot_schedule.h new file mode 100644 index 0000000000000000000000000000000000000000..9c50b6e7d38c0ddc4e574b40a4f3336916e8cc40 --- /dev/null +++ b/common/inc/iot_schedule.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_SCHEDULE_H__ +#define __IOT_SCHEDULE_H__ + +int IOT_ScheduleInit(void); + +int IOT_ScheduleAdd(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value); + +int IOT_ScheduleUpdate(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value); + +int IOT_ScheduleDelete(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value); + +int IOT_ScheduleIsUpdate(unsigned int weekday, int curtime); + +void IOT_ScheduleSetUpdate(int update); + +int IOT_ScheduleGetStartTime(void); + +int IOT_ScheduleGetDurationTime(void); + +int IOT_ScheduleGetCommand(int *cmd, int *value); + +void IOT_ScheduleDeinit(void); + +#endif /* __IOT_SCHEDULE_H__ */ diff --git a/common/inc/iot_sntp.h b/common/inc/iot_sntp.h new file mode 100644 index 0000000000000000000000000000000000000000..94c48321359fd21130689ca7682b7180892dc391 --- /dev/null +++ b/common/inc/iot_sntp.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_SNTP_H +#define __IOT_SNTP_H + +#include + +/** + * @brief Additional introduction for the key members in struct tm + * struct timeval maybe defines as following + * int tm_sec; seconds (0 - 60) + * int tm_min; minutes (0 - 59) + * int tm_hour; hours (0 - 23) + * int tm_mday; day of month (1 - 31) + * int tm_mon; month of year (0 - 11) + * int tm_year; year - 1900 + * int tm_wday; day of week (Sunday = 0) + * int tm_yday; day of year (0 - 365) + * int tm_isdst; is summer time in effect? + * char *tm_zone; abbreviation of timezone name + * long tm_gmtoff; offset from UTC in seconds + * + */ + +/** + * @brief Get the ntp time from ntp server (local RTC TIME);Could only be used after the + * Network is ok and the tcpip is initialized ok + * + * @param localTimeZone indicates the local timezone, and could be -11 to 11, 0 means the UTC+0, + * positive means east while negative means west, 8 means UTC+8, like China + * @param rtcTime is a pointer to storage the rtc time if the return value is 0, and must not + * be NULL + * @return Returns 0 success while -1 failed; The content in rtcTime Only be valid when returns 0 + * + */ +int SntpGetRtcTime(int localTimeZone, struct tm *rtcTime); + + +#endif /* __IOT_SNTP_H */ diff --git a/common/inc/iot_softap.h b/common/inc/iot_softap.h new file mode 100644 index 0000000000000000000000000000000000000000..606166464ca51f1cc52f22d1f5b6f285f63418f6 --- /dev/null +++ b/common/inc/iot_softap.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_SOFTAP_H__ +#define __IOT_SOFTAP_H__ + +/** + * @brief start ap mode. + * + * @param ap_name the ap name + * + * @since 1.0 + * @version 1.0 + * + * @return 0 success, -1 failed + */ +int BOARD_SoftApStart(const char *ap_name); + +/** + * @brief stop ap mode. + * + * @since 1.0 + * @version 1.0 + * + */ +void BOARD_SoftApStop(void); + +#endif /* __IOT_SOFTAP_H__ */ diff --git a/common/inc/iot_softap_xradio.h b/common/inc/iot_softap_xradio.h new file mode 100644 index 0000000000000000000000000000000000000000..606166464ca51f1cc52f22d1f5b6f285f63418f6 --- /dev/null +++ b/common/inc/iot_softap_xradio.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_SOFTAP_H__ +#define __IOT_SOFTAP_H__ + +/** + * @brief start ap mode. + * + * @param ap_name the ap name + * + * @since 1.0 + * @version 1.0 + * + * @return 0 success, -1 failed + */ +int BOARD_SoftApStart(const char *ap_name); + +/** + * @brief stop ap mode. + * + * @since 1.0 + * @version 1.0 + * + */ +void BOARD_SoftApStop(void); + +#endif /* __IOT_SOFTAP_H__ */ diff --git a/common/inc/iot_spi.h b/common/inc/iot_spi.h new file mode 100644 index 0000000000000000000000000000000000000000..663c1b8e33071831f1172c38027f2a08535ff3ec --- /dev/null +++ b/common/inc/iot_spi.h @@ -0,0 +1,368 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup IotHardware + * @{ + * + * @brief Provides APIs for operating devices, + * including flash, GPIO, I2C, PWM, UART, and watchdog APIs. + * + * + * + * @since 2.2 + * @version 2.2 + */ + +/** + * @file iot_spi.h + * + * @brief Declares flash functions. + * + * These functions are used to initialize or deinitialize a flash device, + * and read data from or write data to a flash memory. \n + * + * @since 2.2 + * @version 2.2 + */ + +#ifndef IOT_SPI_H +#define IOT_SPI_H + +/** + * @brief Indicates the SPI callback, which is used in {@link SpiRegisterUsrFunc}. + */ +typedef void (*SpiIsrFunc)(void); + +/** + * @brief Enumerates SPI channel IDs. + */ +typedef enum { + /** Channel 0 */ + IOT_SPI_ID_0 = 0, + /** Channel 1 */ + IOT_SPI_ID_1, +} IotSpiIdx; + +/** + * @brief Enumerates communication polarities. + */ +typedef enum { + /** Polarity 0 */ + IOT_SPI_CFG_CLOCK_CPOL_0, + /** Polarity 1 */ + IOT_SPI_CFG_CLOCK_CPOL_1, +}IotSpiCfgClockCpol; + +/** + * @brief Enumerates communication phases. + */ +typedef enum { + /** Phase 0 */ + IOT_SPI_CFG_CLOCK_CPHA_0, + /** Phase 1 */ + IOT_SPI_CFG_CLOCK_CPHA_1, +} IotSpiCfgClockCpha; + +/** + * @brief Enumerates communication protocols. + */ +typedef enum { + /** Motorola protocol */ + IOT_SPI_CFG_FRAM_MODE_MOTOROLA, + /** Texas Instruments protocol */ + IOT_SPI_CFG_FRAM_MODE_TI, + /** Microwire protocol */ + IOT_SPI_CFG_FRAM_MODE_MICROWIRE, +} IotSpiCfgFramMode; + +/** + * @brief Enumerates the communication data width, that is, + * the number of valid bits in each frame. + * + */ +typedef enum { + /** 4 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_4BIT = 0x3, + /** 5 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_5BIT, + /** 6 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_6BIT, + /** 7 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_7BIT, + /** 8 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_8BIT, + /** 9 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_9BIT, + /** 10 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_10BIT, + /** 11 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_11BIT, + /** 12 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_12BIT, + /** 13 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_13BIT, + /** 14 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_14BIT, + /** 15 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_15BIT, + /** 16 bits */ + IOT_SPI_CFG_DATA_WIDTH_E_16BIT, +} IotSpiCfgDataWidth; + +/** + * @brief Enumerates the endian mode of each frame. + */ +typedef enum { + /** Little-endian */ + IOT_SPI_CFG_ENDIAN_LITTLE, + /** Big-endian */ + IOT_SPI_CFG_ENDIAN_BIG, +} IotSpiCfgEndian; + +/** + * @brief Defines data communication parameters. + */ +typedef struct { + /** Communication polarity. For details about available values, + * see {@link IotSpiCfgClockCpol}. + */ + unsigned int cpol : 1; + /** Communication phase. + * For details about available values, see {@link IotSpiCfgClockCpha}. + */ + unsigned int cpha : 1; + /** Communication protocol. + * For details about available values, see {@link IotSpiCfgFramMode}. + */ + unsigned int framMode : 2; + /** Communication data width. + * For details about available values, see {@link IotSpiCfgDataWidth}. + */ + unsigned int dataWidth : 4; + /** Endian mode. For details about available values, see {@link IotSpiCfgEndian}. */ + unsigned int endian : 1; + /** Padding bit */ + unsigned int pad : 23; + /** Communication frequency. The value ranges from 2460 Hz to 40 MHz. */ + unsigned int freq; +} IotSpiCfgBasicInfo; + +/** + * @brief Specifies whether a device is a master or slave device. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + /** Whether the device is a slave device */ + unsigned int isSlave : 1; + /** Padding bit */ + unsigned int pad : 31; +} IotSpiCfgInitParam; + +/** + * @brief Sends data in SPI slave mode. + * + * In SPI slave mode, this function sends data of the length + * specified by byteLen in writeData through + * the channel specified by spiId within the duration timeOutMs. + * + * @param spiId Indicates the SPI channel ID. + * @param writeData Indicates the pointer to the data to send. + * @param byteLen Indicates the length of the data to send. + * @param timeOutMs Indicates the timeout interval. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiSlaveWrite(IotSpiIdx spiId, char *writeData, unsigned int byteLen, unsigned int timeOutMs); + +/** + * @brief Reads data in SPI slave mode. + * + * In SPI slave mode, this function reads data of the length + * specified by byteLen in readData through the channel + * specified by spiId within the duration timeOutMs. + * + * @param spiId Indicates the SPI channel ID. + * @param readData Indicates the pointer to the data to read. + * @param byteLen Indicates the length of the data to read. + * @param timeOutMs Indicates the timeout interval. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiSlaveRead(IotSpiIdx spiId, char *readData, unsigned int byteLen, unsigned int timeOutMs); + +/** + * @brief Sends data in half-duplex SPI master mode. + * + * In SPI master mode, this function sends data of the length + * specified by byteLen in writeData + * through the channel specified by spiId. + * + * @param spiId Indicates the SPI channel ID. + * @param writeData Indicates the pointer to the data to send. + * @param byteLen Indicates the length of the data to send. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiHostWrite(IotSpiIdx spiId, char *writeData, unsigned int byteLen); + +/** + * @brief Reads data in half-duplex SPI master mode. + * + * In SPI master mode, this function reads data of the length + * specified by byteLen in readData + * through the channel specified by spiId. + * + * @param spiId Indicates the SPI channel ID. + * @param readData Indicates the pointer to the data to read. + * @param byteLen Indicates the length of the data to read. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiHostRead(IotSpiIdx spiId, char *readData, unsigned int byteLen); + +/** + * @brief Sends and reads data in full-duplex SPI master mode. + * + * In SPI master mode, this function sends data in writeData and + * reads data of the length specified by byteLen in readData + * both through the channel specified by spiId. + * + * @param spiId Indicates the SPI channel ID. + * @param writeData Indicates the pointer to the data to send. + * @param readData Indicates the pointer to the data to read. + * @param byteLen Indicates the length of the data to read. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiHostWriteread(IotSpiIdx spiId, char *writeData, char *readData, unsigned int byteLen); + +/** + * @brief Sets the SPI channel parameter. + * + * + * + * @param spiId Indicates the SPI channel ID. + * @param param Indicates the pointer to the SPI parameter to set. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiSetBasicInfo(IotSpiIdx spiId, const IotSpiCfgBasicInfo *param); + +/** + * @brief Initializes an SPI device. + * + * This function initializes the device with the channel ID spiId, + * device type initParam, and device parameter param. + * + * @param spiId Indicates the SPI channel ID. + * @param initParam Specifies whether the device is a slave one. + * @param param Indicates the pointer to the SPI device parameter. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiInit(IotSpiIdx spiId, IotSpiCfgInitParam initParam, const IotSpiCfgBasicInfo *param); + +/** + * @brief Deinitializes an SPI device. + * + * @param spiId Indicates the SPI channel ID. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiDeinit(IotSpiIdx spiId); + +/** + * @brief Sets whether to enable the interrupt request (IRQ) mode for an SPI device. + * + * + * + * @param spiId Indicates the SPI channel ID. + * @param irqEn Specifies whether to enable IRQ. + * The value 1 means to enable IRQ, and 0 means to disable IRQ. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiSetIrqMode(IotSpiIdx spiId, unsigned char irqEn); + +/** + * @brief Sets whether to enable DMA to transfer data for an SPI device in slave mode. + * + * + * + * @param spiId Indicates the SPI channel ID. + * @param dmaEn Specifies whether to enable DMA. + * The value 1 means to enable DMA, and 0 means to disable DMA. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiSetDmaMode(IotSpiIdx spiId, unsigned char dmaEn); + +/** + * @brief Registers the data TX preparation/recovery function. + * + * This function registers the functions + * registered by prepareF and restoreF for + * an SPI device with a channel specified by spiId. + * + * @param spiId Indicates the SPI channel ID. + * @param prepareF Indicates the function used for data preparation. + * @param restoreF Indicates the function used for data recovery. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiRegisterUsrFunc(IotSpiIdx spiId, SpiIsrFunc prepareF, SpiIsrFunc restoreF); + +/** + * @brief Sets whether to enable loopback test for an SPI device. + * + * + * + * @param spiId Indicates the SPI channel ID. + * @param lbEn Specifies whether to enable loopback test. The value 1 + * means to enable loopback test, and 0 means to disable loopback test. + * @return Returns {@link IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link iot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoTSpiSetLoopBackMode(IotSpiIdx spiId, unsigned char lbEn); + +#endif +/** @} */ diff --git a/common/inc/iot_store_manager.h b/common/inc/iot_store_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..447da36ab55df65ebd635035635d01ae914d44f2 --- /dev/null +++ b/common/inc/iot_store_manager.h @@ -0,0 +1,38 @@ + +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __IOT_STORE_MANAGER_H__ +#define __IOT_STORE_MANAGER_H__ + +#define USER_KV_STORE 1 +#if (USER_KV_STORE) +int StoreManagerAddData(const char *data, int length); + +int StoreManagerUpdateData(int idx, const char *data, int length); + +int StoreManagerDeleteDataWithId(int idx); + +int StoreManagerDeleteData(const char *data); + +int StoreManagerGetTotal(void); + +int StoreManagerGetData(char *data, int size, int idx); + +void StoreManagerDelete(void); + +#endif + +#endif /* __IOT_STORE_MANAGER_H__ */ diff --git a/common/inc/iot_wifi.h b/common/inc/iot_wifi.h new file mode 100644 index 0000000000000000000000000000000000000000..387b02cfb0e7777ff14b45779cc5bf89405a750d --- /dev/null +++ b/common/inc/iot_wifi.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IOT_WIFI_H_ +#define IOT_WIFI_H_ + +/** + * @brief Initialize the board wifi + * @return Returns 0 success while others failed + */ +int BOARD_InitWifi(void); + +/** + * @brief Connect to the wifi + * @param wifiSSID Indicates the ssid of the ap + * @param wifiPWD Indicates the pwd of the ap + * @return Returns 0 success while others failed + */ +int BOARD_ConnectWifi(const char *wifiSSID, const char *wifiPWD); + +/** + * @brief Disconnect from the wifi AP + * @return Returns 0 success while others failed + */ +int BOARD_DisconnectWifi(void); + +int BOARD_GetWifiSignalLevel(void); + +#endif /* IOT_WIFI_H_ */ diff --git a/common/inc/iot_wifi_xradio.h b/common/inc/iot_wifi_xradio.h new file mode 100644 index 0000000000000000000000000000000000000000..011ff13ef87b94de6bbe9008408d824a065ecb20 --- /dev/null +++ b/common/inc/iot_wifi_xradio.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IOT_WIFI_H_ +#define IOT_WIFI_H_ + +/** + * @brief Initialize the board wifi + * @return Returns 0 success while others failed + */ +int BOARD_InitWifi(void); + +/** + * @brief Connect to the wifi + * @param wifiSSID Indicates the ssid of the ap + * @param wifiPWD Indicates the pwd of the ap + * @return Returns 0 success while others failed + */ +int BOARD_ConnectWifi(const char *wifiSSID, const char *wifiPWD); + +/** + * @brief Disconnect from the wifi AP + * @return Returns 0 success while others failed + */ +int BOARD_DisconnectWifi(void); + +#endif /* IOT_WIFI_H_ */ diff --git a/common/inc/network_config_service.h b/common/inc/network_config_service.h new file mode 100644 index 0000000000000000000000000000000000000000..8af5174362a89584d7b038aed7a40845d5f372ad --- /dev/null +++ b/common/inc/network_config_service.h @@ -0,0 +1,375 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup NetcfgService + * @{ + * + * @brief Provides the network configuration service. Two network configuration modes are available: + * Neighbor Awareness Networking (NAN) and software enabled access point (SoftAP). + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file network_config_service.h + * + * @brief Defines functions of the NetcfgService module. + * + * You can use the functions to: \n + *
    + *
  • Start the network configuration service.
  • + *
  • Stop the network configuration service.
  • + *
  • Register callbacks for events of the network configuration service.
  • + *
  • Notify the network configuration result.
  • + *
+ * + * @since 1.0 + * @version 1.0 + */ + +#ifndef NETWORK_CONFIG_SERVICE_H +#define NETWORK_CONFIG_SERVICE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Represents the maximum length (32 bytes) of a SoftAP hotspot name. + * + */ +#define SSID_MAX_LEN 32 + +/** + * @brief Represents the maximum length (64 bytes) of a SoftAP hotspot password. + * + */ +#define PWD_MAX_LEN 64 + +/** + * @brief Represents the maximum length (32 bytes) of a Wi-Fi Service Set Identifier (SSID). + * + */ +#define WIFI_SSID_MAX_LEN 32 + +/** + * @brief Represents the maximum length (64 bytes) of the password for Wi-Fi configuration. + * + */ +#define WIFI_PWD_MAX_LEN 64 + +/** + * @brief Represents the maximum length (32 bytes) of the pre-shared key for Wi-Fi configuration. + * + */ +#define WIFI_PSK_LEN 32 + +/** + * @brief Represents the maximum length (64 bytes) of the Basic Service Set Identifiers (BSSID) \n + * for Wi-Fi configuration. + * + */ +#define WIFI_BSSID_LEN 6 + +/** + * @brief Enumerates Wi-Fi encryption types. + * + */ +enum WifiPairwise { + /** UNKNOWN */ + WIFI_PARIWISE_UNKNOWN, + /** AES */ + WIFI_PAIRWISE_AES, + /** TKIP */ + WIFI_PAIRWISE_TKIP, + /** TKIP and AES */ + WIFI_PAIRWISE_TKIP_AES_MIX +}; + +/** + * @brief Enumerates Wi-Fi authentication types. + * + */ +enum WifiAuthMode { + /** Open */ + WIFI_SECURITY_OPEN, + /** WEP */ + WIFI_SECURITY_WEP, + /** WPA2-PSK */ + WIFI_SECURITY_WPA2PSK, + /** WPA-PSK and WPA2-PSK */ + WIFI_SECURITY_WPAPSK_WPA2PSK_MIX, + /** WPA-PSK */ + WIFI_SECURITY_WPAPSK, + /** WPA */ + WIFI_SECURITY_WPA, + /** WPA2 */ + WIFI_SECURITY_WPA2, + /** SAE */ + WIFI_SECURITY_SAE, + /** UNKNOWN */ + WIFI_SECURITY_UNKNOWN +}; + +/** + * @brief Enumerates network configuration types. + * + */ +enum NetCfgType { + /** SoftAP */ + NETCFG_SOFTAP = 0, + /** SoftAP and NAN */ + NETCFG_SOFTAP_NAN, + /** Reserved */ + NETCFG_BUTT, +}; + +/** + * @brief Enumerates common error codes of the NetcfgService module. + * + */ +enum NetworkCfgErrorCode { + /** Success */ + NETCFG_OK = 0, + /** Execution failure */ + NETCFG_ERROR = -1, + /** Invalid device information */ + NETCFG_DEV_INFO_INVALID = -2, + /** Invalid SoftAP network configuration parameters */ + NETCFG_SOFTAP_PARAM_INVALID = -3, + /** Incorrect network configuration mode */ + NETCFG_MODE_INVALID = -4, + /** Invalid power for short-distance transmission */ + NETCFG_POWER_INVALID = -5, +}; + +/** + * @brief Enumerates information transmission channels for Wi-Fi configuration. + * + */ +enum WifiInfoSource { + /** Wi-Fi network configuration information is transmitted through NAN channels. */ + WIFI_INFO_FROM_NAN = 0, + /** Wi-Fi network configuration information is transmitted through SoftAP channels. */ + WIFI_INFO_FROM_SOFTAP, +}; + +/** + * @brief Enumerates network configuration service statuses. + * + */ +enum NetCfgStatus { + /** In NAN mode, blinking or buzzing stops when the network configuration is complete or times out. */ + NETCFG_IDENTIFY_DISABLE = 0, + /** In NAN mode, the indicator blinks or the buzzer buzzes when a device is authenticated for access. */ + NETCFG_IDENTIFY_ENABLE = 1, + /** The device is in network configuration. */ + NETCFG_WORKING = 2 +}; + +/** + * @brief Defines SoftAP hotspot parameters. + */ +struct SoftAPParam { + /** (Mandatory) Hotspot name. For details about the maximum length, see {@link SSID_MAX_LEN}. */ + char ssid[SSID_MAX_LEN + 1]; + /** (Optional) Hotspot password. For details about the maximum length, see {@link PWD_MAX_LEN}. + * If the password is empty, the SoftAP is in open mode. + * If the password is not empty, the password length must be less than or equal to the value of PWD_MAX_LEN. + */ + char password[PWD_MAX_LEN + 1]; + /** (Optional) Authentication type + * If the password is empty, the authentication mode must be open. + * If the password is specified, the authentication type can be WEP, WPA2-PSK, or both. + */ + int authType; + /** (Optional) Encryption type. + * Currently, UNKNOWN, AES, TKIP, and TKIP and AES encryption types are supported. + */ + int pairwise; + /** (Optional) Whether to hide the SSID. + * 0 means not to hide the SSID, and 1 means to hide the SSID. + */ + int isHidden; + /** (Optional) Working channel required in the SoftAP mode + * 0: The channel is specified by the system. 1 to 14: The channel is specified by users. + */ + int channelNum; +}; + +/** + * @brief Defines device parameters, which are passed when the application starts the network configuration service. + */ +struct DevInfo { + /** Device parameter name, which contains a maximum of 32 bytes. */ + const char *key; + /** Device parameter value, which contains a maximum of 64 bytes. */ + const char *value; +}; + +/** + * @brief Defines the SSID name, password, and channel used for Wi-Fi configuration. + */ +struct WifiInfo { + /** SSID name. For details about the maximum length, see {@link WIFI_SSID_MAX_LEN}. */ + unsigned char ssid[WIFI_SSID_MAX_LEN + 1]; + /** SSID password. For details about the maximum length, see {@link WIFI_PWD_MAX_LEN}. */ + unsigned char pwd[WIFI_PWD_MAX_LEN + 1]; + /** Pre-shared key. For details about the maximum length, see {@link WIFI_PSK_LEN}. */ + unsigned char psk[WIFI_PSK_LEN + 1]; + /** BSSID. For details about the maximum length, see {@link WIFI_BSSID_LEN}. */ + unsigned char bssid[WIFI_BSSID_LEN + 1]; + /** Length of the SSID name */ + unsigned char ssidLen; + /** Length of the SSID password */ + unsigned char pwdLen; + /** Length of the pre-shared key */ + unsigned char pskLen; + /** Length of the BSSID */ + unsigned char bssidLen; + /** Authentication type used for Wi-Fi configuration + * If the password is empty, the authentication mode must be open. + * If the password is specified, the authentication type can be WEP, WPA2-PSK, or both. + */ + int authMode; + /** Channel for transmitting information. For details about the value, see {@link WifiInfoSource}. */ + int wifiInfoSrc; + /** Working channel used for Wi-Fi configuration. The value ranges from 1 to 14. */ + int channelNumber; +}; + +/** + * @brief Defines callbacks for network configuration registration. + */ +typedef struct { + /** Obtains the PIN code of the device. The maximum length of the PIN code is 16 bytes. */ + int (*GetPinCode)(unsigned char *pinCode, unsigned int size, unsigned int *len); + /** Processes Wi-Fi information and vendor data during network configuration */ + int (*ParseNetCfgData)(const struct WifiInfo *wifiInfo, + const unsigned char *vendorData, unsigned int vendorDataLen); + /** Processes application rapid control data in the rapid control phase. + * The svcId parameter is reserved for device compatibility. The default value is NULL. + * The mode parameter is reserved. The default value is 0. + */ + int (*RecvRawData)(const char *svcId, unsigned int mode, const char *data); + /** Notifies the network configuration service status. \n + * For example, when a device is in NAN mode, this function can be called to control the blinking or buzzer used \n + * for notifications. \n + * status indicates the network configuration status. \n + * The value 0 means to stop buzzing or blinking. \n + * The value 1 means to start buzzing or blinking. \n + * The value 2 means that the device is in the network configuration state. \n + */ + void (*NotifyNetCfgStatus)(enum NetCfgStatus status); + /** Disconnects the network configuration. \n + * This function is used to notify the application when an internal error occurs during network configuration \n + * or a disconnection request is received from the mobile phone. \n + * The errCode values are as follows: \n + * 0: The application fails to receive the network configuration data. \n + * The network configuration service automatically starts. \n + * 1: The application has been notified of the network configuration data and needs to determine \n + * whether to stop or restart the network configuration service. \n + */ + void (*OnDisconnect)(int errCode); +} NetCfgCallback; + +/** + * @brief Sets the transmit channel power for ultra-short-distance transmission \n + * before the network configuration service is started. + * + * @param power Indicates the power for ultra-short-distance transmission. \n + * The value can be -70, -67, -65, -63, -61, -58, -55, \n + * -52, -48, -45, or -42. To prevent a mobile phone far away from the device \n + * from receiving security information, ensure that the wireless transmit power at any point \n + * on the product surface (including the external antenna) does not exceed -65 dBm. + * @return Returns the operation result. For details, see {@link NetworkCfgErrorCode}. + * @since 1.0 + * @version 1.0 + */ +int SetSafeDistancePower(signed char power); + +/** + * @brief Sets SoftAP hotspot parameters. + * + * @param param Indicates the pointer to the structure of the hotspot parameters. For details, see {@link SoftAPParam}. + * @return Returns the operation result. For details, see {@link NetworkCfgErrorCode}. + * @since 1.0 + * @version 1.0 + */ +int SetSoftAPParameter(const struct SoftAPParam *param); + +/** + * @brief Registers a callback. + * + * Call this function before enabling the network configuration service. + * + * @param callback Indicates the pointer to the structure of the callback function. \n + * For details, see {@link NetCfgCallback}. + * @return Returns the operation result. For details, see {@link NetworkCfgErrorCode}. + * @since 1.0 + * @version 1.0 + */ +int RegNetCfgCallback(const NetCfgCallback *callback); + +/** + * @brief Starts a network configuration. + * + * Packets with the multicast address 238.238.238.238 and port number 5683 are listened by default. + * Note: In NAN mode, call this function after the SoftAP is started successfully. + * + * @param devInfoList Indicates the pointer to the device information list. For details, see {@link DevInfo}. + * @param listSize Indicates the number of devices in the device information list. + * @param mode Indicates the working mode of the network configuration service. For details, see {@link NetcfgPara}. + * @return Returns the operation result. For details, see {@link NetworkCfgErrorCode}. + * @since 1.0 + * @version 1.0 + */ +int StartNetCfg(const struct DevInfo *devInfoList, unsigned int listSize, enum NetCfgType mode); + +/** + * @brief Stops the network configuration. + * + * @return Returns the operation result. For details, see {@link NetworkCfgErrorCode}. + * @since 1.0 + * @version 1.0 + */ +int StopNetCfg(void); + +/** + * @brief Notifies the network configuration result. + * + * @param result Indicates the network configuration result. + * @return Returns the operation result. For details, see {@link NetworkCfgErrorCode}. + * @since 1.0 + * @version 1.0 + */ +int NotifyNetCfgResult(signed int result); + +/** + * @brief Sends data. + * + * @param data Indicates the pointer to the data to send. The data is a character string. + * @return Returns the operation result. For details, see {@link NetworkCfgErrorCode}. + * @since 1.0 + * @version 1.0 + */ +int SendRawData(const char *data); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/common/inc/peripheral_hal.h b/common/inc/peripheral_hal.h new file mode 100644 index 0000000000000000000000000000000000000000..370cd942c8fcd2b155c772b6cc389deb87105d88 --- /dev/null +++ b/common/inc/peripheral_hal.h @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PERIPHERAL_HAL_H +#define PERIPHERAL_HAL_H + +#include "ohos_types.h" + +/* gpio start */ +typedef enum { + /** GPIO hardware pin 0 */ + HAL_WIFI_IOT_IO_NAME_GPIO_0, + /** GPIO hardware pin 1 */ + HAL_WIFI_IOT_IO_NAME_GPIO_1, + /** GPIO hardware pin 2 */ + HAL_WIFI_IOT_IO_NAME_GPIO_2, + /** GPIO hardware pin 3 */ + HAL_WIFI_IOT_IO_NAME_GPIO_3, + /** GPIO hardware pin 4 */ + HAL_WIFI_IOT_IO_NAME_GPIO_4, + /** GPIO hardware pin 5 */ + HAL_WIFI_IOT_IO_NAME_GPIO_5, + /** GPIO hardware pin 6 */ + HAL_WIFI_IOT_IO_NAME_GPIO_6, + /** GPIO hardware pin 7 */ + HAL_WIFI_IOT_IO_NAME_GPIO_7, + /** GPIO hardware pin 8 */ + HAL_WIFI_IOT_IO_NAME_GPIO_8, + /** GPIO hardware pin 9 */ + HAL_WIFI_IOT_IO_NAME_GPIO_9, + /** GPIO hardware pin 10 */ + HAL_WIFI_IOT_IO_NAME_GPIO_10, + /** GPIO hardware pin 11 */ + HAL_WIFI_IOT_IO_NAME_GPIO_11, + /** GPIO hardware pin 12 */ + HAL_WIFI_IOT_IO_NAME_GPIO_12, + /** GPIO hardware pin 13 */ + HAL_WIFI_IOT_IO_NAME_GPIO_13, + /** GPIO hardware pin 14 */ + HAL_WIFI_IOT_IO_NAME_GPIO_14, + /** Maximum value */ + HAL_WIFI_IOT_IO_NAME_MAX, +} HalWifiIotIoName; + +/** + * @brief set IO function. + * + * @param id -- IO number, reference {@ HalWifiIotIoName}. + * @param val -- the io function value which defined in {@ hi_io.h}. + * + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int HalIoSetFunc(HalWifiIotIoName id, const char *val); +/* gpio end */ + + +/* adc start */ + +/** + * @brief Enumerates ADC channel indexes. + * + */ +typedef enum { + /** Channel 0 */ + HAL_WIFI_IOT_ADC_CHANNEL_0, + /** Channel 1 */ + HAL_WIFI_IOT_ADC_CHANNEL_1, + /** Channel 2 */ + HAL_WIFI_IOT_ADC_CHANNEL_2, + /** Channel 3 */ + HAL_WIFI_IOT_ADC_CHANNEL_3, + /** Channel 4 */ + HAL_WIFI_IOT_ADC_CHANNEL_4, + /** Channel 5 */ + HAL_WIFI_IOT_ADC_CHANNEL_5, + /** Channel 6 */ + HAL_WIFI_IOT_ADC_CHANNEL_6, + /** Channel 7 */ + HAL_WIFI_IOT_ADC_CHANNEL_7, + /** Button value */ + HAL_WIFI_IOT_ADC_CHANNEL_BUTT, +} HalWifiIotAdcChannelIndex; + +/** + * @brief Enumerates analog power control modes. + */ +typedef enum { + /** Automatic control */ + HAL_WIFI_IOT_ADC_CUR_BAIS_DEFAULT, + /** Automatic control */ + HAL_WIFI_IOT_ADC_CUR_BAIS_AUTO, + /** Manual control (AVDD = 1.8 V) */ + HAL_WIFI_IOT_ADC_CUR_BAIS_1P8V, + /** Manual control (AVDD = 3.3 V) */ + HAL_WIFI_IOT_ADC_CUR_BAIS_3P3V, + /** Button value */ + HAL_WIFI_IOT_ADC_CUR_BAIS_BUTT, +} HalWifiIotAdcCurBais; + +/** + * @brief Enumerates equation models. + */ +typedef enum { + /** One-equation model */ + HAL_WIFI_IOT_ADC_EQU_MODEL_1, + /** Two-equation model */ + HAL_WIFI_IOT_ADC_EQU_MODEL_2, + /** Four-equation model */ + HAL_WIFI_IOT_ADC_EQU_MODEL_4, + /** Eight-equation model */ + HAL_WIFI_IOT_ADC_EQU_MODEL_8, + /** Button value */ + HAL_WIFI_IOT_ADC_EQU_MODEL_BUTT, +} HalWifiIotAdcEquModelSel; + +/** + * @brief Reads a piece of sampled data from a specified ADC channel based on the input parameters. + * + * + * + * @param channel Indicates the ADC channel index. + * @param data Indicates the pointer to the address for storing the read data. + * @param equModel Indicates the equation model. + * @param curBais Indicates the analog power control mode. + * @param rstCnt Indicates the count of the time from reset to conversion start. + * One count is equal to 334 ns. The value must range from 0 to 0xFF0. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int HalAdcRead(HalWifiIotAdcChannelIndex channel, unsigned short *data, HalWifiIotAdcEquModelSel equModel, + HalWifiIotAdcCurBais curBais, unsigned short rstCnt); +/** adc end ****/ + +/** + * @brief set WatchDog enable or disable. + * + * @param enable -- 1 enable, 0 disable. + * + * @since 1.0 + * @version 1.0 + */ +void HalSetWatchDogEnable(int enable); + +/** + * @brief start the pwm. + * + * @param id Port id of PWM + * @param duty The usefull cycles of PWM + * @param freq The total cycles of PWM + * + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int HalPwmStart(uint32 id, uint16 duty, uint16 freq); + +#endif // PERIPHERAL_HAL_H \ No newline at end of file diff --git a/common/inc/schedule_list.h b/common/inc/schedule_list.h new file mode 100644 index 0000000000000000000000000000000000000000..38a0138a3261f2b435a7a195496418482b58bc51 --- /dev/null +++ b/common/inc/schedule_list.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __SCHEDULE_LIST_H__ +#define __SCHEDULE_LIST_H__ + +#include "schedule_store.h" + +#define SCH_DEBUG +#ifdef SCH_DEBUG +#define SCH_DBG(fmt, args...) printf("[DEBUG][%s|%d]" fmt, __func__, __LINE__, ##args) +#define SCH_ERR(fmt, args...) printf("[ERROR][%s|%d]" fmt, __func__, __LINE__, ##args) +#else +#define SCH_DBG(fmt, args...) do{}while(0) +#define SCH_ERR(fmt, args...) do{}while(0) +#endif + +typedef void* SCHEDULE_LIST; + +SCHEDULE_LIST ScheduleListCreate(ScheduleInfo *info); + +int ScheduleListAppend(SCHEDULE_LIST mHandle, ScheduleInfo *info); + +int ScheduleListUpdate(SCHEDULE_LIST mHandle, ScheduleInfo *info); + +int ScheduleListDelete(SCHEDULE_LIST mHandle, ScheduleInfo *info); + +int ScheduleListGetSize(SCHEDULE_LIST mHandle); + +int ScheduleListGet(SCHEDULE_LIST mHandle, int idx, ScheduleInfo *info); + +void ScheduleListDestroy(SCHEDULE_LIST mHandle); + +#endif /* __SCHEDULE_LIST_H__ */ diff --git a/common/inc/schedule_store.h b/common/inc/schedule_store.h new file mode 100644 index 0000000000000000000000000000000000000000..5bb84cb149e4162c85895e6c33611cb6e4f29f3a --- /dev/null +++ b/common/inc/schedule_store.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __SCHEDULE_STORE_H__ +#define __SCHEDULE_STORE_H__ + +#define SCHEDULE_ID_LENGTH 40 + +typedef struct { + unsigned char cmd; + unsigned int value; +}ScheduleCommand; + +typedef struct { + unsigned char week; + unsigned int starttime; + unsigned int duration; + unsigned char id[SCHEDULE_ID_LENGTH]; + + ScheduleCommand scheduleCmd; +} ScheduleInfo; + +typedef enum { + SCHEDULE_OPTION_ADD = 0, + SCHEDULE_OPTION_UPDATE, + SCHEDULE_OPTION_DELETE +} SCHEDULE_OPTION; + +int ScheduleStoreUpdate(ScheduleInfo *info, SCHEDULE_OPTION option); + +int ScheduleStoreGetTotal(void); + +int ScheduleStoreGetInfo(ScheduleInfo *info, int idx); + +void ScheduleStoreDelete(void); + +#endif /* __SCHEDULE_STORE_H__ */ diff --git a/common/inc/utils_hal.h b/common/inc/utils_hal.h new file mode 100644 index 0000000000000000000000000000000000000000..fb158c285296d1f53a06127802d4137125f91a4e --- /dev/null +++ b/common/inc/utils_hal.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __UTILS_HAL_H__ +#define __UTILS_HAL_H__ + +void hal_udelay(uint32 us); + +#endif // __UTILS_HAL_H__ \ No newline at end of file diff --git a/common/iot_boardbutton/BUILD.gn b/common/iot_boardbutton/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0f6a28da715a4fecf79d1e473ac06d7fcee6fa47 --- /dev/null +++ b/common/iot_boardbutton/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_boardbutton") { + sources = [ + "iot_boardbutton.c", + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "../inc", + ] + + deps = [ ] + +} \ No newline at end of file diff --git a/common/iot_boardbutton/iot_boardbutton.c b/common/iot_boardbutton/iot_boardbutton.c new file mode 100644 index 0000000000000000000000000000000000000000..e1692a1a82c1385b2be63e6d2f05ecf1051b5bc7 --- /dev/null +++ b/common/iot_boardbutton/iot_boardbutton.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iot_boardbutton.h" +#include "cmsis_os2.h" +#include "iot_gpio.h" +#include "iot_gpio_ex.h" +#include "ohos_init.h" + +typedef enum { + BUTTON_ID_F1 = 0, + BUTTON_ID_F2, + BUTTON_ID_MAX, +}ButtonID; + +typedef struct { + ButtonPressedCallback callback; + char *args; +}ButtonCallBackController; + +typedef struct { + ButtonCallBackController controller[BUTTON_ID_MAX]; +}ButtonController; +static ButtonController g_buttonController; + + + +#define BUTTON_F1_GPIO 11 +#define BUTTON_F2_GPIO 12 + +/** + * @brief Callback for F1 key + * + */ +static void F1Pressed(char* arg) +{ + (void)arg; + printf("%s:pressed \r\n", __FUNCTION__); + if (g_buttonController.controller[BUTTON_ID_F1].callback != NULL) { + g_buttonController.controller[BUTTON_ID_F1].callback(arg); + } +} + +/** + * @brief Callback for F2 key + * + */ +static void F2Pressed(char* arg) +{ + printf("%s:pressed \r\n", __FUNCTION__); + if (g_buttonController.controller[BUTTON_ID_F2].callback != NULL) { + g_buttonController.controller[BUTTON_ID_F2].callback(arg); + } +} + +int Board_IsButtonPressedF2(void) +{ + int value = 1; + int tmcnt = 10; + + IoTGpioInit(BUTTON_F2_GPIO); + IoTGpioSetFunc(BUTTON_F2_GPIO, IOT_GPIO_FUNC_GPIO_12_GPIO); + IoTGpioSetDir(BUTTON_F2_GPIO, IOT_GPIO_DIR_IN); + + while (--tmcnt > 0) { + osDelay(20); + if (IoTGpioGetInputVal(BUTTON_F2_GPIO, &value) != 0) { + printf("[ERROR][%s|%d] IoTGpioGetInputVal failed! \n", __func__, __LINE__); + break; + } + if (value) { + break; + } + } + + return value ? 0 : 1; +} + +int Board_InitButtonF1(ButtonPressedCallback callback, char *arg) +{ + g_buttonController.controller[BUTTON_ID_F1].callback = callback; + g_buttonController.controller[BUTTON_ID_F1].args = arg; + // init gpio of F1 key and set it as the falling edge to trigger interrupt + IoTGpioInit(BUTTON_F1_GPIO); + IoTGpioSetFunc(BUTTON_F1_GPIO, IOT_GPIO_FUNC_GPIO_11_GPIO); + IoTGpioSetDir(BUTTON_F1_GPIO, IOT_GPIO_DIR_IN); + IoTGpioSetPull(BUTTON_F1_GPIO, IOT_GPIO_PULL_UP); + IoTGpioRegisterIsrFunc(BUTTON_F1_GPIO, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1Pressed, NULL); + + return 0; +} + +int Board_InitButtonF2(ButtonPressedCallback callback, char *arg) +{ + g_buttonController.controller[BUTTON_ID_F2].callback = callback; + g_buttonController.controller[BUTTON_ID_F2].args = arg; + // init gpio of F2 key and set it as the falling edge to trigger interrupt + IoTGpioInit(BUTTON_F2_GPIO); + IoTGpioSetFunc(BUTTON_F2_GPIO, IOT_GPIO_FUNC_GPIO_12_GPIO); + IoTGpioSetDir(BUTTON_F2_GPIO, IOT_GPIO_DIR_IN); + IoTGpioSetPull(BUTTON_F2_GPIO, IOT_GPIO_PULL_UP); + IoTGpioRegisterIsrFunc(BUTTON_F2_GPIO, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, F2Pressed, NULL); + + return 0; +} + + + + + diff --git a/common/iot_boardbutton_xradio/BUILD.gn b/common/iot_boardbutton_xradio/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e5faa754a6c9198be7094be99d159914b51af9c9 --- /dev/null +++ b/common/iot_boardbutton_xradio/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +source_set("iot_boardbutton") { + sources = [ + "iot_boardbutton_xradio.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "//kernel/liteos_m/kal/cmsis", + "../inc", + "../hals/inc", + "//drivers/framework/include/platform", + "//drivers/framework/include/utils", + "//drivers/adapter/khdf/liteos_m/osal/include", + "//drivers/framework/include/core", + "//drivers/framework/include/osal" + ] + +} \ No newline at end of file diff --git a/common/iot_boardbutton_xradio/iot_boardbutton_xradio.c b/common/iot_boardbutton_xradio/iot_boardbutton_xradio.c new file mode 100644 index 0000000000000000000000000000000000000000..d09a36e2d6b2df3248590960737c76d807a1b114 --- /dev/null +++ b/common/iot_boardbutton_xradio/iot_boardbutton_xradio.c @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include + +#include "iot_boardbutton_xradio.h" +#include "gpio_if.h" +#include "cmsis_os2.h" +#include "ohos_init.h" + +#define KEY_OK 1 +#define TASK_SIZE 1024 +#define LONG_PRESS_TIME 2000 + +#define USER_DEF 0 +#define USER_XR 1 + +#define CS_SELECT USER_XR + +#if (CS_SELECT == USER_XR) +#define KEY_BUTTON_IO 11 +#else +#define KEY_BUTTON_IO 0 +#endif + +#define SLEEP_10MS 10000 +#define SLEEP_100MS 100000 +#define MINISECONDS_IN_SECOND 1000 + +#define BTTN_DBG(fmt, arg...) printf("[%s|%d][BUTTON_DEBUG]" fmt, __func__, __LINE__, ##arg) +#define BTTN_ERR(fmt, arg...) printf("[%s|%d][BUTTON_ERROR]" fmt, __func__, __LINE__, ##arg) + +static ButtonPressedCallback g_ButtonPressedCallback = NULL; +static unsigned char g_button_state = BUTTON_INVALID; +static OS_Thread_t g_button_thread; + +static unsigned long GetMillionSecond(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + + return tv.tv_sec * MINISECONDS_IN_SECOND + tv.tv_usec / MINISECONDS_IN_SECOND; // 1000 is millionsecond +} + +static int KeyIrqCallback(int gpio, void *arg) +{ + BTTN_DBG("KeyIrqCallback! gpio = %d \n", gpio); + return 0; +} + +static int g_threadRunning = 0; + +static void ButtonEventThread(void *arg) +{ + ButtonEvent *event = (ButtonEvent *)arg; + if (g_ButtonPressedCallback != NULL) { + g_ButtonPressedCallback(event); + } + g_threadRunning = 0; + BTTN_DBG("g_threadRunning = %d \n", g_threadRunning); +} + +static void ButtonEventHandle(ButtonEvent *event) +{ + BTTN_DBG("g_threadRunning = %d \n", g_threadRunning); + if (g_threadRunning == 0) { + const int priority = 25; + const int stack_size = (1024 * 2); + osThreadAttr_t attr; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = stack_size; + attr.priority = priority; + attr.name = "ButtonEventThread"; + g_threadRunning = 1; + if (osThreadNew((osThreadFunc_t)ButtonEventThread, event, (const osThreadAttr_t *)&attr) == NULL) { + BTTN_ERR("create ButtonEventThread failed! \n"); + g_threadRunning = 0; + return; + } + } +} + +static void ButtonProcess(void *args) +{ + unsigned long pressTime = 0; + ButtonEvent event; + + GpioSetIrq(KEY_BUTTON_IO, GPIO_IRQ_TRIGGER_FALLING, KeyIrqCallback, NULL); + GpioEnableIrq(KEY_BUTTON_IO); + while (1) { + int value = 0; + if (GpioRead(KEY_BUTTON_IO, &value) < 0) { + BTTN_ERR("read io(%d) failed! \n", KEY_BUTTON_IO); + sleep(1); + continue; + } + + if (value == 0) { // button pressed! + if (g_button_state == BUTTON_PRESS) { + if (pressTime == 0) { + pressTime = GetMillionSecond(); + } + if ((GetMillionSecond() - pressTime) >= LONG_PRESS_TIME) { + event.button_id = KEY_BUTTON_IO; + event.value = BUTTON_LONG_PRESS; + g_button_state = BUTTON_LONG_PRESS; + ButtonEventHandle(&event); + } + } else { + if (g_button_state != BUTTON_LONG_PRESS) { + g_button_state = BUTTON_PRESS; + } + } + } else { // button release or no button pressed! + if (g_button_state == BUTTON_PRESS) { + event.button_id = KEY_BUTTON_IO; + event.value = BUTTON_PRESS; + ButtonEventHandle(&event); + } + if (g_button_state != BUTTON_RELEASE) { + pressTime = 0; + g_button_state = BUTTON_RELEASE; + } + } + usleep(SLEEP_10MS); + } +} + +int Board_ButtonInit(ButtonPressedCallback callback) +{ + g_ButtonPressedCallback = callback; + if (OS_ThreadCreate(&g_button_thread, "ButtonProcess", ButtonProcess, + NULL, OS_THREAD_PRIO_APP, TASK_SIZE) != OS_OK) { + BTTN_ERR("OS_ThreadCreate failed! \n"); + return -1; + } + return 0; +} + +int Board_IsButtonPressed(void) +{ + int tmCount = 20; + int result = 1; + while (tmCount-- > 0) { + int value = 0; + if (GpioRead(KEY_BUTTON_IO, &value) < 0) { + BTTN_ERR("read io(%d) failed! \n", KEY_BUTTON_IO); + result = 0; + break; + } + if (value == 1) { + BTTN_DBG("key have not pressed!\n"); + result = 0; + break; + } + usleep(SLEEP_100MS); + } + + return result; +} diff --git a/common/iot_boardled/BUILD.gn b/common/iot_boardled/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0280facf478fa9f1e5b28dc088a02770157c8d0f --- /dev/null +++ b/common/iot_boardled/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_boardled") { + sources = [ + "iot_boardled.c", + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "../inc", + ] + + deps = [ ] + +} diff --git a/common/iot_boardled/iot_boardled.c b/common/iot_boardled/iot_boardled.c new file mode 100644 index 0000000000000000000000000000000000000000..c70b1b4dab05e3c8908248bab1929a5bf47d543d --- /dev/null +++ b/common/iot_boardled/iot_boardled.c @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iot_demo_def.h" +#include "iot_boardled.h" +#include "iot_gpio.h" +#include "iot_gpio_ex.h" +#include "iot_pwm.h" +#include "ohos_init.h" +#include + + +#define DEVICE_STATUS_LED_GPIO 2 +#define DEVICE_SATTUS_LED_PWD 2 +#define CONFIG_FLASHLED_FREDEFAULT 5 + +/** + * @brief Initialize the board pwd led + * @return Returns 0 success while others failed + */ +int BOARD_InitPwmLed(void) +{ + IoTGpioInit(DEVICE_STATUS_LED_GPIO); + IoTGpioSetFunc(DEVICE_STATUS_LED_GPIO, IOT_GPIO_FUNC_GPIO_2_PWM2_OUT); + IoTGpioSetDir(DEVICE_STATUS_LED_GPIO, IOT_GPIO_DIR_OUT); + IoTPwmInit(DEVICE_SATTUS_LED_PWD); + + return 0; +} + +/** + * @brief Set Pwm led duty cycle + * @param dutyCycle, the duty cycle to set, not max than 40000 + * @return Returns 0 success while others failed + */ +int BOARD_SetPwmLedDutyCycle(int dutyCycle) +{ + dutyCycle = dutyCycle < 0 ? 1 : dutyCycle; + dutyCycle = dutyCycle >= 100 ? 99 : dutyCycle; + IoTPwmStart(DEVICE_STATUS_LED_GPIO, dutyCycle, 40000); + return 0; +} + +// the led part +int BOARD_InitIoLed(void) +{ + //设置GPIO_2的复用功能为普通GPIO + IoTGpioSetFunc(DEVICE_STATUS_LED_GPIO, IOT_GPIO_FUNC_GPIO_2_GPIO); + //设置GPIO_2为输出模式 + IoTGpioSetDir(DEVICE_STATUS_LED_GPIO, IOT_GPIO_DIR_OUT); + return 0; +} + +int BOARD_SetIoLedStatus(int status) +{ + if (status) { + IoTGpioSetOutputVal(DEVICE_STATUS_LED_GPIO, 1); + } else { + IoTGpioSetOutputVal(DEVICE_STATUS_LED_GPIO, 0); + } + return 0; +} + + +#define CONFIG_TASK_MAIN_STACKSIZE 0x800 // main task stacksize must be bigger +#define CONFIG_TASK_MAIN_PRIOR 22 // default task priority + +/** + * @brief Convert miniseconds to system ticks + * @param ms Indicates the mimiseconds to convert + * @return Returns the corresponding ticks of specified time + */ +static uint32_t Time2Tick(uint32_t ms) +{ + uint64_t ret; + ret = ((uint64_t)ms * osKernelGetTickFreq()) / CN_MINISECONDS_IN_SECOND; + return (uint32_t)ret; +} + + +/** + @ brief Set the main task flash frequency + */ + +typedef struct +{ + int cycleTicks; + int cycleLightTicks; + osThreadId_t taskID; +}LedFlashController; +static LedFlashController g_ledFlashController; + +#define CN_LED_FLASH_HIGHBASE 200 +#define CN_LED_FLASH_CYCLEBASE 1000 +int LedFlashFrequencySet(float flashHz) +{ + int cycleHighMs = 0; + int cycleMs = 0; + + cycleMs = (int)(CN_MINISECONDS_IN_SECOND / flashHz); + cycleHighMs = cycleMs *CN_LED_FLASH_HIGHBASE /CN_LED_FLASH_CYCLEBASE; + + g_ledFlashController.cycleTicks = Time2Tick(cycleMs); + g_ledFlashController.cycleLightTicks = Time2Tick(cycleHighMs); + RaiseLog(LOG_LEVEL_INFO, "cycle:%d cycleon:%d ",g_ledFlashController.cycleTicks, g_ledFlashController.cycleLightTicks); + return 0; +} + + +int LedFlashTaskDeinit(void) +{ + osThreadTerminate(g_ledFlashController.taskID); + g_ledFlashController.taskID = NULL; + return 0; +} + +/** + * @brief LED flashing task entry + */ +static void LedTaskEntry(const void *arg) +{ + (void)arg; + BOARD_InitIoLed(); + LedFlashFrequencySet(CONFIG_FLASHLED_FREDEFAULT); + while (1) { + BOARD_SetIoLedStatus(CN_BOARD_SWITCH_ON); + osDelay((uint32_t)g_ledFlashController.cycleLightTicks); + BOARD_SetIoLedStatus(CN_BOARD_SWITCH_OFF); + osDelay((uint32_t)(g_ledFlashController.cycleTicks - g_ledFlashController.cycleLightTicks)); + } + return; +} + +static void LedFlashTaskInit(void) +{ + osThreadAttr_t attr; + RaiseLog(LOG_LEVEL_INFO, "DATA:%s Time:%s \r\n", __DATE__, __TIME__); + // Create the IoT Main task + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = CONFIG_TASK_MAIN_STACKSIZE; + attr.priority = CONFIG_TASK_MAIN_PRIOR; + attr.name = "LedFlashTask"; + g_ledFlashController.taskID = osThreadNew((osThreadFunc_t)LedTaskEntry, NULL, (const osThreadAttr_t *)&attr); + if (g_ledFlashController.taskID == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Create the LED FLASH TASK failed"); + } else { + RaiseLog(LOG_LEVEL_INFO, "Create the LED FLASH TASK success"); + } + + return; +} + +APP_FEATURE_INIT(LedFlashTaskInit); \ No newline at end of file diff --git a/common/iot_boardled_xradio/BUILD.gn b/common/iot_boardled_xradio/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..cdde8cda1bfc08d87540221a3010041d4036e3ec --- /dev/null +++ b/common/iot_boardled_xradio/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_boardled_xradio") { + sources = [ + "iot_boardled_xradio.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "../inc", + ] + + deps = [ ] + +} diff --git a/common/iot_boardled_xradio/iot_boardled_xradio.c b/common/iot_boardled_xradio/iot_boardled_xradio.c new file mode 100644 index 0000000000000000000000000000000000000000..7bd39612f8e28c19c55a855675dc634cddac78bd --- /dev/null +++ b/common/iot_boardled_xradio/iot_boardled_xradio.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iot_demo_def.h" +#include "iot_boardled_xradio.h" +#include "ohos_init.h" +#include + +#define DEVICE_STATUS_LED_GPIO 21 // PA21 + +// the led part +int BOARD_InitIoLed(void) +{ + printf("LED INIT! \n"); + return 0; +} + +int BOARD_SetIoLedStatus(int status) +{ + if (status) { + GpioWrite(DEVICE_STATUS_LED_GPIO, 1); + } else { + GpioWrite(DEVICE_STATUS_LED_GPIO, 0); + } + return 0; +} + +#define CONFIG_TASK_MAIN_STACKSIZE 0x800 // main task stacksize must be bigger +#define CONFIG_TASK_MAIN_PRIOR 22 // default task priority + +/** + @ brief Set the main task flash frequency + */ + +typedef struct { + int mode; + osThreadId_t taskID; +}LedFlashController; +static LedFlashController g_ledFlashController; + +int LedFlashFrequencySet(LED_FLASH_MODE mode) +{ + g_ledFlashController.mode = mode; + + return 0; +} + +int LedFlashTaskDeinit(void) +{ + osThreadTerminate(g_ledFlashController.taskID); + g_ledFlashController.taskID = NULL; + return 0; +} + +/** + * @brief LED flashing task entry + */ +static void LedTaskEntry(const void *arg) +{ + const int ledFlashTime[LED_FLASH_MAX] = { + 100000, 300000, 500000, 1000000, 5000000 + }; + (void)arg; + BOARD_InitIoLed(); + LedFlashFrequencySet(LED_FLASH_FAST_PLUGS); + while (1) { + if (g_ledFlashController.mode > LED_FLASH_SLOW_PLUGS) { + g_ledFlashController.mode = LED_FLASH_SLOW_PLUGS; + } + BOARD_SetIoLedStatus(CN_BOARD_SWITCH_ON); + if (g_ledFlashController.mode == LED_FLASH_SLOW_PLUGS) { + usleep(ledFlashTime[LED_FLASH_SLOW]); + } else { + usleep(ledFlashTime[g_ledFlashController.mode]); + } + BOARD_SetIoLedStatus(CN_BOARD_SWITCH_OFF); + usleep(ledFlashTime[g_ledFlashController.mode]); + } + return; +} + +static void LedFlashTaskInit(void) +{ + osThreadAttr_t attr; + RaiseLog(LOG_LEVEL_INFO, "DATA:%s Time:%s \r\n", __DATE__, __TIME__); + // Create the IoT Main task + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = CONFIG_TASK_MAIN_STACKSIZE; + attr.priority = CONFIG_TASK_MAIN_PRIOR; + attr.name = "LedFlashTask"; + g_ledFlashController.taskID = osThreadNew((osThreadFunc_t)LedTaskEntry, NULL, (const osThreadAttr_t *)&attr); + if (g_ledFlashController.taskID == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Create the LED FLASH TASK failed"); + } else { + RaiseLog(LOG_LEVEL_INFO, "Create the LED FLASH TASK success"); + } + + return; +} + +void LedFlashTaskStart(void) +{ + LedFlashTaskInit(); +} diff --git a/common/iot_cloud/BUILD.gn b/common/iot_cloud/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..08b57a17f2fe650c83e1ce0a886d72cf6ebb70b4 --- /dev/null +++ b/common/iot_cloud/BUILD.gn @@ -0,0 +1,37 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_cloud") { + sources = [ + "iot_cloud.c", + "iot_profile_package.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//third_party/cJSON", + "//third_party/iot_link/inc", + "//third_party/iot_link/oc_mqtt/oc_mqtt_al", + "//third_party/iot_link/oc_mqtt/oc_mqtt_profile_v5", + "//third_party/iot_link/queue", + "../inc", + ] + + deps = [ + "//third_party/iot_link:iot_link", + ] + +} diff --git a/common/iot_cloud/iot_cloud.c b/common/iot_cloud/iot_cloud.c new file mode 100644 index 0000000000000000000000000000000000000000..afa47c788f37c1c213d7f7df305d6b3eae9c52e2 --- /dev/null +++ b/common/iot_cloud/iot_cloud.c @@ -0,0 +1,333 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iot_cloud.h" +#include +#include +#include +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include +#include +#include +#include +#include +#include + + +// we use this function to send the data here +extern int OcMqttReportString(char *deviceID, const char *jsonString); +extern int OcMqttMsgUpString(char *deviceID, const char *jsonString); +typedef enum { + EN_MSG_TYPE_CMD = 0, + EN_MSG_TYPE_REPORT, + EN_MSG_TYPE_CONN, + EN_MSG_TYPE_DISCONN, + EN_MSG_TYPE_NOTIFY, +}CloudMsgType; + +typedef struct +{ + char *requestID; + char *payload; +}CloudCommandMsg; + +typedef struct { + char *jsonString; +}CloudReportMsg; + +typedef struct { + CloudMsgType msgType; + union { + CloudCommandMsg cmd; + CloudReportMsg report; + CloudReportMsg notify; + }msg; +}ApplicationMsg; + +typedef struct +{ + queue_t *applicationMsgQueue; + int connected; + osThreadId_t mainTaskID; +}CloudController; + + +#define CONFIG_APP_LIFETIME 60 ///< seconds +#define CONFIG_QUEUE_TIMEOUT (5*1000) +static CloudController g_cloudController; + +static void HuaweiIoTSDKInit(void) +{ + dtls_al_init(); + mqtt_al_init(); + oc_mqtt_init(); +} + +/** + * @brief This function used to report the data to the cloud + * +*/ +static void DealReportMsg(CloudReportMsg *reportMsg) +{ + OcMqttReportString(NULL, reportMsg->jsonString); + return; +} + +static void DealNotificationMsg(CloudReportMsg *notifyMsg) +{ + OcMqttMsgUpString(NULL, notifyMsg->jsonString); +} + +/** + * @brief This function used to deal with message received from the cloud + * we package the received data to the queue to do next step +*/ +static int CloudMsgRcvCallBack(oc_mqtt_profile_msgrcv_t *msg) +{ + int ret = 0; + char *buf = NULL; + int bufLen; + ApplicationMsg *applicationMsg = NULL; + + if ((NULL == msg) || (msg->request_id == NULL) || (msg->type != EN_OC_MQTT_PROFILE_MSG_TYPE_DOWN_COMMANDS)) { + RaiseLog(LOG_LEVEL_WARN,"Parameter is wrong format\n"); + return ret; + } + + bufLen = sizeof(ApplicationMsg) + strlen(msg->request_id) + 1 + msg->msg_len + 1; + buf = malloc(bufLen); + if (NULL == buf) { + RaiseLog(LOG_LEVEL_ERR,"No memory for the command buffer\n"); + return ret; + } + applicationMsg = (ApplicationMsg *)buf; + buf += sizeof(ApplicationMsg); + + applicationMsg->msgType = EN_MSG_TYPE_CMD; + applicationMsg->msg.cmd.requestID = buf; + bufLen = strlen(msg->request_id); + buf += bufLen + 1; + memcpy(applicationMsg->msg.cmd.requestID, msg->request_id, bufLen); + applicationMsg->msg.cmd.requestID[bufLen] = '\0'; + bufLen = msg->msg_len; + applicationMsg->msg.cmd.payload = buf; + memcpy(applicationMsg->msg.cmd.payload, msg->msg, bufLen); + applicationMsg->msg.cmd.payload[bufLen] = '\0'; + RaiseLog(LOG_LEVEL_INFO,"GetCommand:reqID:%s payload:%s \n", \ + applicationMsg->msg.cmd.requestID, applicationMsg->msg.cmd.payload); + + ret = queue_push(g_cloudController.applicationMsgQueue,applicationMsg,10); + if (ret != 0){ + free(applicationMsg); + RaiseLog(LOG_LEVEL_ERR,"Push the message to the queue failed"); + } + return ret; +} + +static void DealCommandMsg(CloudCommandMsg *cmd) +{ + oc_mqtt_profile_cmdresp_t cmdresp; + + int ret = CLOUD_CommandCallBack((const char *)(cmd->payload)); + ///< do the response + cmdresp.paras = NULL; + cmdresp.request_id = cmd->requestID; + cmdresp.ret_code = (ret == 0 ? 0 : 1); + cmdresp.ret_name = NULL; + (void)oc_mqtt_profile_cmdresp(NULL, &cmdresp); + return; +} + +/** + * @brief this is the cloud main task entry + * we deal all the message in the queue +*/ +static int CloudMainTaskEntry(void *arg) +{ + ApplicationMsg *applicationMsg; + uint32_t ret ; + // receive the message from the queue ,maybe receive from the clould, or maybe from the local + while (1) { + applicationMsg = NULL; + (void)queue_pop(g_cloudController.applicationMsgQueue,(void **)&applicationMsg,0xFFFFFFFF); + if (applicationMsg != NULL) { + RaiseLog(LOG_LEVEL_INFO,"GetMsgType:%d",applicationMsg->msgType); + switch (applicationMsg->msgType){ + case EN_MSG_TYPE_CMD: + DealCommandMsg(&applicationMsg->msg.cmd); + break; + case EN_MSG_TYPE_REPORT: + DealReportMsg(&applicationMsg->msg.report); + break; + case EN_MSG_TYPE_NOTIFY: + DealNotificationMsg(&applicationMsg->msg.notify); + break; + default: + break; + } + free(applicationMsg); + } + } + return 0; +} + + +int CLOUD_Init(void) +{ + int ret = -1; + /* create a queue to buffer the data */ + g_cloudController.applicationMsgQueue = queue_create("queue_rcvmsg",10,1); + if(NULL == g_cloudController.applicationMsgQueue){ + printf("Create receive msg queue failed"); + return ret; + } + /* initialize the iot sdk */ + HuaweiIoTSDKInit(); + /* create a task to deal the send message or received message */ + osThreadAttr_t attr; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.name = "IoTCloudMain"; + attr.stack_size = 1024*8; + attr.priority = 24; + if ((g_cloudController.mainTaskID = osThreadNew((osThreadFunc_t)CloudMainTaskEntry, NULL, &attr)) == NULL) { + RaiseLog(LOG_LEVEL_ERR,"Could not create the iotcloud thread"); + return ret; + } + ret = 0; + return ret; +} + +int CLOUD_Deinit(void) +{ + int ret = -1; + osThreadTerminate(g_cloudController.mainTaskID); + g_cloudController.mainTaskID = NULL; + queue_delete( g_cloudController.applicationMsgQueue); + g_cloudController.applicationMsgQueue = NULL; + return ret; +} + +int CLOUD_Connect(const char *deviceID, const char *devicePwd, \ + const char *serverIP, const char *serverPort) +{ + int ret; + oc_mqtt_profile_connect_t connect_para; + (void) memset( &connect_para, 0, sizeof(connect_para)); + + connect_para.boostrap = 0; + connect_para.device_id = (char *)deviceID; + connect_para.device_passwd = (char *)devicePwd; + connect_para.server_addr = (char *)serverIP; + connect_para.server_port = (char *)serverPort; + connect_para.life_time = CONFIG_APP_LIFETIME; + connect_para.rcvfunc = CloudMsgRcvCallBack; + connect_para.security.type = EN_DTLS_AL_SECURITY_TYPE_NONE; + ret = oc_mqtt_profile_connect(&connect_para); + if ((ret == (int)en_oc_mqtt_err_ok)) { + g_cloudController.connected = 1; + ret = 0; + } + else { + RaiseLog(LOG_LEVEL_ERR,"Could not connect to the huaweiIotPlatform:deviceID:%s devicePwd:%s serverIP:%s serverPort:%s", \ + deviceID, devicePwd, serverIP, serverPort); + ret = -1; + } + return ret; +} + +int CLOUD_Disconnect(void) +{ + int ret; + ret = oc_mqtt_profile_disconnect(); + if (ret == (int)en_oc_mqtt_err_ok) { + return 0; + } else { + return -1; + } +} + +int CLOUD_ReportMsg(const char *jsonString) +{ + int ret = -1; + ApplicationMsg *applicationMsg = NULL; + + if (jsonString == NULL) { + RaiseLog(LOG_LEVEL_WARN,"Parameter jsonString is NULL"); + return ret; + } + + applicationMsg = malloc(sizeof(ApplicationMsg) + strlen(jsonString) + 1); + if (NULL != applicationMsg) { + applicationMsg->msgType = EN_MSG_TYPE_REPORT; + applicationMsg->msg.report.jsonString = (char *)applicationMsg + sizeof(ApplicationMsg); + strcpy(applicationMsg->msg.report.jsonString, jsonString); + if (0 != queue_push(g_cloudController.applicationMsgQueue, applicationMsg, CONFIG_QUEUE_TIMEOUT)) { + free(applicationMsg); + RaiseLog(LOG_LEVEL_ERR,"Could not push message to the message queue"); + } else { + ret = 0; + } + } else { + RaiseLog(LOG_LEVEL_ERR,"Could not get the memory for the application message"); + } + return ret; +} + +int CLOUD_ReportNotification(int type, const char *enString, const char *chString) +{ + int ret = -1; + ApplicationMsg *applicationMsg = NULL; + char *jsonString = NULL; + if (type >= NOTIFY_TYPE_LAST || enString == NULL || chString == NULL) { + RaiseLog(LOG_LEVEL_WARN,"Parameter failed!\n"); + return -1; + } + + jsonString = IotNotificationPackage(type, enString, chString); + if (jsonString == NULL) { + RaiseLog(LOG_LEVEL_WARN,"IotNotificationPackage jsonString failed! \n"); + return ret; + } + + applicationMsg = malloc(sizeof(ApplicationMsg) + strlen(jsonString) + 1); + if (NULL != applicationMsg) { + applicationMsg->msgType = EN_MSG_TYPE_NOTIFY; + applicationMsg->msg.report.jsonString = (char *)applicationMsg + sizeof(ApplicationMsg); + strcpy(applicationMsg->msg.report.jsonString, jsonString); + if (0 != queue_push(g_cloudController.applicationMsgQueue, applicationMsg, CONFIG_QUEUE_TIMEOUT)) { + free(applicationMsg); + RaiseLog(LOG_LEVEL_ERR,"Could not push message to the message queue"); + } else { + ret = 0; + } + } else { + RaiseLog(LOG_LEVEL_ERR,"Could not get the memory for the application message"); + } + + free(jsonString); + + return ret; +} + +int CLOUD_GetCloudConnectedStatus(void) +{ + return g_cloudController.connected; +} diff --git a/common/iot_cloud/iot_profile_package.c b/common/iot_cloud/iot_profile_package.c new file mode 100644 index 0000000000000000000000000000000000000000..fde5627820ac0fc0d3dda4500cb83d322258fcf6 --- /dev/null +++ b/common/iot_cloud/iot_profile_package.c @@ -0,0 +1,303 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iot_cloud.h" +#include "cJSON.h" + +#define IOT_PROFILE_KEY_SERVICES "services" +#define IOT_PROFILE_KEY_SERVICEID "service_id" +#define IOT_PROFILE_KEY_PROPERTIES "properties" +#define IOT_PROFILE_KEY_EVENTTIME "event_time" +#define IOT_PROFILE_KEY_SERVICES "services" + +// format the report data to json string mode +static cJSON *FormatValue(IotProfileKV *kv) +{ + cJSON *ret = NULL; + switch (kv->type) + { + case IOT_PROFILE_KEY_DATATYPE_INT: + ret = cJSON_CreateNumber((double)(*(int *)kv->value)); + break; + case IOT_PROFILE_KEY_DATATYPE_LONG: + ret = cJSON_CreateNumber((double)(*(long *)kv->value)); + break; + case IOT_PROFILE_KEY_DATATYPE_FLOAT: + ret = cJSON_CreateNumber((double)(*(float *)kv->value)); + break; + case IOT_PROFILE_KEY_DATATYPE_DOUBLE: + ret = cJSON_CreateNumber((*(double *)kv->value)); + break; + case IOT_PROFILE_KEY_DATATYPE_STRING: + ret = cJSON_CreateString((const char *)kv->value); + break; + default: + break; + } + + return ret; +} + + +static cJSON *FormatProperty(IotProfileKV *propertyLst) +{ + cJSON *rootObj = NULL; + cJSON *propertyObj = NULL; + IotProfileKV *property = NULL; + // build a root node + rootObj = cJSON_CreateObject(); + if (rootObj == NULL) { + goto EXIT_MEM; + } + // add all the property to the properties + property = propertyLst; + while (NULL != property) { + propertyObj = FormatValue(property); + if (propertyObj == NULL) { + goto EXIT_MEM; + } + + cJSON_AddItemToObject(rootObj, property->key, propertyObj); + property = property->nxt; + } + + // OK, now we return it + return rootObj; + +EXIT_MEM: + if (NULL != rootObj) { + cJSON_Delete(rootObj); + rootObj = NULL; + } + return rootObj; + +} + +static cJSON *FormatService(IotProfileService *service) +{ + cJSON *rootObj; + cJSON *serviceID; + cJSON *propertyObj; + cJSON *eventTimeObj; + + // build a rootObj node + rootObj = cJSON_CreateObject(); + if (NULL == rootObj) { + goto EXIT_MEM; + } + // add the serviceID node to the rootObj node + serviceID = cJSON_CreateString(service->serviceID); + if (NULL == serviceID) { + goto EXIT_MEM; + } + cJSON_AddItemToObjectCS(rootObj, IOT_PROFILE_KEY_SERVICEID, serviceID); + + // add the propertyObj node to the rootObj + propertyObj = FormatProperty(service->propertyLst); + if (NULL == propertyObj) { + goto EXIT_MEM; + } + cJSON_AddItemToObjectCS(rootObj, IOT_PROFILE_KEY_PROPERTIES, propertyObj); + // add the event time (optional) to the rootObj + if (NULL != service->eventTime) { + eventTimeObj = cJSON_CreateString(service->eventTime); + if (NULL == eventTimeObj) { + goto EXIT_MEM; + } + cJSON_AddItemToObjectCS(rootObj, IOT_PROFILE_KEY_EVENTTIME, eventTimeObj); + } + // OK, now we return it + return rootObj; + +EXIT_MEM: + if (NULL != rootObj) { + cJSON_Delete(rootObj); + rootObj = NULL; + } + return rootObj; +} + +static cJSON *FormatServices(IotProfileService *serviceLst) +{ + cJSON *rootObj = NULL; + cJSON * serviceObj; + IotProfileService *service; + + // create the rootObj array node + rootObj = cJSON_CreateArray(); + if (NULL == rootObj) { + goto EXIT_MEM; + } + service = serviceLst; + while (NULL != service) { + serviceObj = FormatService(service); + if (NULL == serviceObj) { + goto EXIT_MEM; + } + + cJSON_AddItemToArray(rootObj, serviceObj); + service = service->nxt; + } + + // now we return the rootObj + return rootObj; + +EXIT_MEM: + if (NULL != rootObj) { + cJSON_Delete(rootObj); + rootObj = NULL; + } + return rootObj; +} + +char *IoTProfilePackage(IotProfileService *serviceLst) +{ + char *ret = NULL; + cJSON *rootObj; + cJSON *servicesObj; + + // create the rootObj node + rootObj = cJSON_CreateObject(); + if (NULL == rootObj) { + goto EXIT_MEM; + } + // create the services array node to the rootObj + servicesObj = FormatServices(serviceLst); + if (NULL == servicesObj) { + goto EXIT_MEM; + } + cJSON_AddItemToObjectCS(rootObj, IOT_PROFILE_KEY_SERVICES, servicesObj); + // OK, now we make it to a buffer + ret = cJSON_PrintUnformatted(rootObj); + cJSON_Delete(rootObj); + return ret; + +EXIT_MEM: + if (NULL != rootObj) { + cJSON_Delete(rootObj); + } + return ret; +} + + +static cJSON *NotifyGetMsgObj(const char *enString, const char *chString) +{ + cJSON *objRoot; + cJSON *objEn, *objCh; + char *ret = NULL; + + objRoot = cJSON_CreateObject(); + if (objRoot == NULL) { + printf("[%s|%d][ERROR] cJSON_CreateObject \n", __func__,__LINE__); + return NULL; + } + + objEn = cJSON_CreateString(enString); + if (objEn == NULL) { + printf("[%s|%d][ERROR] cJSON_CreateString %s\n", __func__,__LINE__,enString); + goto MSG_ERR; + } + cJSON_AddItemToObjectCS(objRoot, "msg_en", objEn); + + objCh = cJSON_CreateString(chString); + if (objCh == NULL) { + printf("[%s|%d][ERROR] cJSON_CreateString %s\n", __func__,__LINE__,chString); + goto MSG_ERR; + } + cJSON_AddItemToObjectCS(objRoot, "msg_zh", objCh); + + return objRoot; +MSG_ERR: + if (objRoot) { + cJSON_Delete(objRoot); + } + return NULL; +} + +static cJSON *NotifyGetParamObj(int type, const char *enString, const char *chString) +{ + cJSON *objRoot; + cJSON *objtype; + cJSON *objmsg; + + objRoot = cJSON_CreateObject(); + if (objRoot == NULL) { + printf("[%s|%d][ERROR] cJSON_CreateObject \n", __func__,__LINE__); + return NULL; + } + + objtype = cJSON_CreateNumber(type); + if (objtype == NULL) { + printf("[%s|%d][ERROR] cJSON_CreateNumber \n", __func__,__LINE__); + goto PARAM_ERR; + } + cJSON_AddItemToObjectCS(objRoot, "AlarmType", objtype); + + objmsg = NotifyGetMsgObj(enString, chString); + if (objmsg == NULL) { + goto PARAM_ERR; + printf("[%s|%d][ERROR] NotifyGetMsgObj \n", __func__,__LINE__); + } + cJSON_AddItemToObjectCS(objRoot, "ALarmMsg", objmsg); + + return objRoot; +PARAM_ERR: + if (objRoot) { + cJSON_Delete(objRoot); + } + return NULL; +} +char *IotNotificationPackage(int type, const char *enString, const char *chString) +{ + char *ret = NULL; + cJSON *rootObj; + cJSON *serviceidObjs; + cJSON *propertiesObj; + cJSON *objtype; + cJSON *objmsg; + cJSON *objEn, *objCh; + + // create the rootObj node + rootObj = cJSON_CreateObject(); + if (NULL == rootObj) { + printf("[%s|%d][ERROR] cJSON_CreateObject \n", __func__,__LINE__); + goto EXIT_MEM; + } + // create the services array node to the rootObj + serviceidObjs = cJSON_CreateString("DeviceAlarm"); + if (serviceidObjs == NULL) { + printf("[%s|%d][ERROR] cJSON_CreateString DeviceAlarm \n", __func__,__LINE__); + goto EXIT_MEM; + } + cJSON_AddItemToObjectCS(rootObj, "serviceId", serviceidObjs); + + propertiesObj = NotifyGetParamObj(type, enString, chString); + if (propertiesObj == NULL) { + printf("[%s|%d][ERROR] cJSON_CreateObject \n", __func__,__LINE__); + goto EXIT_MEM; + } + cJSON_AddItemToObjectCS(rootObj, "properties", propertiesObj); + + // OK, now we make it to a buffer + ret = cJSON_PrintUnformatted(rootObj); + cJSON_Delete(rootObj); + printf("ret = %s \n", ret); + return ret; +EXIT_MEM: + if (NULL != rootObj) { + cJSON_Delete(rootObj); + } + return NULL; +} diff --git a/common/iot_gpio_interface/BUILD.gn b/common/iot_gpio_interface/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0106489b84b3a27deef0d006315989127bd73c33 --- /dev/null +++ b/common/iot_gpio_interface/BUILD.gn @@ -0,0 +1,21 @@ + +static_library("iot_gpio_interface") { + sources = [ + "iot_gpio_gpio.c", + "iot_gpio_pwm.c", + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "../../../iot_hardware_hals/include", + "../../iot_hardware_hals/include", + "../../include", + ] + + deps = [ ] + +} diff --git a/common/iot_gpio_interface/iot_gpio_func.c b/common/iot_gpio_interface/iot_gpio_func.c new file mode 100644 index 0000000000000000000000000000000000000000..457400c4d438489c55147b489011ff6bc97ada8d --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_func.c @@ -0,0 +1,315 @@ +#include "wifiiot_gpio.h" +#include "iot_gpio_ex.h" +#include "iot_uart.h" +#include "iot_spi.h" +#include "iot_pwm.h" +#include "iot_i2c.h" +#include "iot_gpio_func.h" + + +/* 通过GPIO引脚号获取GPIO功能编号 */ +u32 Gpio2Gpio(u32 gpio, u8 *func) +{ + u32 ret = KP_ERR_SUCCESS; + + /* 根据引脚号确定引脚GPIO功能编号 */ + switch (gpio) + { + case WIFI_IOT_GPIO_IDX_0: + func = IOT_GPIO_FUNC_GPIO_0_GPIO; + break; + case WIFI_IOT_GPIO_IDX_1: + func = IOT_GPIO_FUNC_GPIO_1_GPIO; + break; + case WIFI_IOT_GPIO_IDX_2: + func = IOT_GPIO_FUNC_GPIO_2_GPIO; + break; + case WIFI_IOT_GPIO_IDX_3: + func = IOT_GPIO_FUNC_GPIO_3_GPIO; + break; + case WIFI_IOT_GPIO_IDX_4: + func = IOT_GPIO_FUNC_GPIO_4_GPIO; + break; + case WIFI_IOT_GPIO_IDX_5: + func = IOT_GPIO_FUNC_GPIO_5_GPIO; + break; + case WIFI_IOT_GPIO_IDX_6: + func = IOT_GPIO_FUNC_GPIO_6_GPIO; + break; + case WIFI_IOT_GPIO_IDX_7: + func = IOT_GPIO_FUNC_GPIO_7_GPIO; + break; + case WIFI_IOT_GPIO_IDX_8: + func = IOT_GPIO_FUNC_GPIO_8_GPIO; + break; + case WIFI_IOT_GPIO_IDX_9: + func = IOT_GPIO_FUNC_GPIO_9_GPIO; + break; + case WIFI_IOT_GPIO_IDX_10: + func = IOT_GPIO_FUNC_GPIO_10_GPIO; + break; + case WIFI_IOT_GPIO_IDX_11: + func = IOT_GPIO_FUNC_GPIO_11_GPIO; + break; + case WIFI_IOT_GPIO_IDX_12: + func = IOT_GPIO_FUNC_GPIO_12_GPIO; + break; + case WIFI_IOT_GPIO_IDX_13: + func = IOT_GPIO_FUNC_GPIO_13_GPIO; + break; + case WIFI_IOT_GPIO_IDX_14: + func = IOT_GPIO_FUNC_GPIO_14_GPIO; + break; + default: + IOT_PRINTF_FUNC("Invalid gpio :%d \r\n", gpio); + return KP_ERR_FAILURE; + } + + return KP_ERR_SUCCESS; +} + +/* 通过GPIO引脚号获取GPIO功能编号 */ +u32 Gpio2Uart(u32 gpio, u8 *func, u32 *uartId) +{ + u32 ret = KP_ERR_SUCCESS; + + /* 根据引脚号确定引脚GPIO功能编号 */ + switch (gpio) + { + case WIFI_IOT_GPIO_IDX_0: + func = IOT_GPIO_FUNC_GPIO_0_UART1_TXD; + uartId = IOT_UART_IDX_1; + break; + case WIFI_IOT_GPIO_IDX_1: + func = IOT_GPIO_FUNC_GPIO_1_UART1_RXD; + uartId = IOT_UART_IDX_1; + break; + case WIFI_IOT_GPIO_IDX_3: + func = IOT_GPIO_FUNC_GPIO_3_UART0_TXD; + uartId = IOT_UART_IDX_0; + break; + case WIFI_IOT_GPIO_IDX_4: + func = IOT_GPIO_FUNC_GPIO_4_UART0_RXD; + uartId = IOT_UART_IDX_0; + break; + case WIFI_IOT_GPIO_IDX_5: + func = IOT_GPIO_FUNC_GPIO_5_UART1_RXD; + uartId = IOT_UART_IDX_1; + break; + case WIFI_IOT_GPIO_IDX_6: + func = IOT_GPIO_FUNC_GPIO_6_UART1_TXD; + uartId = IOT_UART_IDX_1; + break; + case WIFI_IOT_GPIO_IDX_11: + func = IOT_GPIO_FUNC_GPIO_11_UART2_TXD; + uartId = IOT_UART_IDX_2; + break; + case WIFI_IOT_GPIO_IDX_12: + func = IOT_GPIO_FUNC_GPIO_12_UART2_RXD; + uartId = IOT_UART_IDX_2; + break; + case WIFI_IOT_GPIO_IDX_13: + func = IOT_GPIO_FUNC_GPIO_13_UART0_TXD; + uartId = IOT_UART_IDX_0; + break; + case WIFI_IOT_GPIO_IDX_14: + func = IOT_GPIO_FUNC_GPIO_14_UART0_RXD; + uartId = IOT_UART_IDX_0; + break; + default: + IOT_PRINTF_FUNC("Invalid gpio :%d \r\n", gpio); + return KP_ERR_FAILURE; + } + + return KP_ERR_SUCCESS; +} + +/* 通过GPIO引脚号获取SPI功能编号 */ +u32 Gpio2Spi(u32 gpio, u8 *func, u32 *spiId) +{ + u32 ret = KP_ERR_SUCCESS; + + /* 根据引脚号确定引脚GPIO功能编号 */ + switch (gpio) + { + case WIFI_IOT_GPIO_IDX_0: + func = IOT_GPIO_FUNC_GPIO_0_SPI1_CK; + spiId = IOT_SPI_ID_1; + break; + case WIFI_IOT_GPIO_IDX_1: + func = IOT_GPIO_FUNC_GPIO_1_SPI1_RXD; + spiId = IOT_SPI_ID_1; + break; + case WIFI_IOT_GPIO_IDX_2: + func = IOT_GPIO_FUNC_GPIO_2_SPI1_TXD; + spiId = IOT_SPI_ID_1; + break; + case WIFI_IOT_GPIO_IDX_3: + func = IOT_GPIO_FUNC_GPIO_3_SPI1_CSN; + spiId = IOT_SPI_ID_1; + break; + case WIFI_IOT_GPIO_IDX_5: + func = IOT_GPIO_FUNC_GPIO_5_SPI0_CSN; + spiId = IOT_SPI_ID_0; + break; + case WIFI_IOT_GPIO_IDX_6: + func = IOT_GPIO_FUNC_GPIO_6_SPI0_CK; + spiId = IOT_SPI_ID_0; + break; + case WIFI_IOT_GPIO_IDX_7: + func = IOT_GPIO_FUNC_GPIO_7_SPI0_RXD; + spiId = IOT_SPI_ID_0; + break; + case WIFI_IOT_GPIO_IDX_8: + func = IOT_GPIO_FUNC_GPIO_8_SPI0_TXD; + spiId = IOT_SPI_ID_0; + break; + case WIFI_IOT_GPIO_IDX_9: + func = IOT_GPIO_FUNC_GPIO_9_SPI0_TXD; + spiId = IOT_SPI_ID_0; + break; + case WIFI_IOT_GPIO_IDX_10: + func = IOT_GPIO_FUNC_GPIO_10_SPI0_CK; + spiId = IOT_SPI_ID_0; + break; + case WIFI_IOT_GPIO_IDX_11: + func = IOT_GPIO_FUNC_GPIO_11_SPI0_RXD; + spiId = IOT_SPI_ID_0; + break; + case WIFI_IOT_GPIO_IDX_12: + func = IOT_GPIO_FUNC_GPIO_12_SPI0_CSN; + spiId = IOT_SPI_ID_0; + break; + default: + IOT_PRINTF_FUNC("Invalid gpio :%d \r\n", gpio); + return KP_ERR_FAILURE; + } + + return KP_ERR_SUCCESS; +} + +/* 通过GPIO引脚号获取PWM功能编号 */ +u32 Gpio2Pwm(u32 gpio, u8 *func, u32 *pwmId) +{ + u32 ret = KP_ERR_SUCCESS; + + /* 根据引脚号确定引脚GPIO功能编号 */ + switch (gpio) + { + case WIFI_IOT_GPIO_IDX_0: + func = IOT_GPIO_FUNC_GPIO_0_PWM3_OUT; + pwmId = PWM_PORT_PWM3; + break; + case WIFI_IOT_GPIO_IDX_1: + func = IOT_GPIO_FUNC_GPIO_1_PWM4_OUT; + pwmId = PWM_PORT_PWM4; + break; + case WIFI_IOT_GPIO_IDX_2: + func = IOT_GPIO_FUNC_GPIO_2_PWM2_OUT; + pwmId = PWM_PORT_PWM2; + break; + case WIFI_IOT_GPIO_IDX_3: + func = IOT_GPIO_FUNC_GPIO_3_PWM5_OUT; + pwmId = PWM_PORT_PWM5; + break; + case WIFI_IOT_GPIO_IDX_4: + func = IOT_GPIO_FUNC_GPIO_4_PWM1_OUT; + pwmId = PWM_PORT_PWM1; + break; + case WIFI_IOT_GPIO_IDX_5: + func = IOT_GPIO_FUNC_GPIO_5_PWM2_OUT; + pwmId = PWM_PORT_PWM2; + break; + case WIFI_IOT_GPIO_IDX_6: + func = IOT_GPIO_FUNC_GPIO_6_PWM3_OUT; + pwmId = PWM_PORT_PWM3; + break; + case WIFI_IOT_GPIO_IDX_7: + func = IOT_GPIO_FUNC_GPIO_7_PWM0_OUT; + pwmId = PWM_PORT_PWM0; + break; + case WIFI_IOT_GPIO_IDX_8: + func = IOT_GPIO_FUNC_GPIO_8_PWM1_OUT; + pwmId = PWM_PORT_PWM1; + break; + case WIFI_IOT_GPIO_IDX_9: + func = IOT_GPIO_FUNC_GPIO_9_PWM0_OUT; + pwmId = PWM_PORT_PWM0; + break; + case WIFI_IOT_GPIO_IDX_10: + func = IOT_GPIO_FUNC_GPIO_10_PWM1_OUT; + pwmId = PWM_PORT_PWM1; + break; + case WIFI_IOT_GPIO_IDX_11: + func = IOT_GPIO_FUNC_GPIO_11_PWM2_OUT; + pwmId = PWM_PORT_PWM2; + break; + case WIFI_IOT_GPIO_IDX_12: + func = IOT_GPIO_FUNC_GPIO_12_PWM3_OUT; + pwmId = PWM_PORT_PWM3; + break; + case WIFI_IOT_GPIO_IDX_13: + func = IOT_GPIO_FUNC_GPIO_13_PWM4_OUT; + pwmId = PWM_PORT_PWM4; + break; + case WIFI_IOT_GPIO_IDX_14: + func = IOT_GPIO_FUNC_GPIO_14_PWM5_OUT; + pwmId = PWM_PORT_PWM5; + break; + default: + IOT_PRINTF_FUNC("Invalid gpio :%d \r\n", gpio); + return KP_ERR_FAILURE; + } + + return KP_ERR_SUCCESS; +} + +/* 通过GPIO引脚号获取IIC功能编号 */ +u32 Gpio2IIC(u32 gpio, u8 *func, u32 *i2cId) +{ + u32 ret = KP_ERR_SUCCESS; + + /* 根据引脚号确定引脚GPIO功能编号 */ + switch (gpio) + { + case WIFI_IOT_GPIO_IDX_0: + func = IOT_GPIO_FUNC_GPIO_0_I2C1_SDA; + i2cId = IOT_I2C_IDX_1; + break; + case WIFI_IOT_GPIO_IDX_1: + func = IOT_GPIO_FUNC_GPIO_1_I2C1_SCL; + i2cId = IOT_I2C_IDX_1; + break; + case WIFI_IOT_GPIO_IDX_3: + func = IOT_GPIO_FUNC_GPIO_3_I2C1_SDA; + i2cId = IOT_I2C_IDX_1; + break; + case WIFI_IOT_GPIO_IDX_4: + func = IOT_GPIO_FUNC_GPIO_4_I2C1_SCL; + i2cId = IOT_I2C_IDX_1; + break; + case WIFI_IOT_GPIO_IDX_9: + func = IOT_GPIO_FUNC_GPIO_9_I2C0_SCL; + i2cId = IOT_I2C_IDX_0; + break; + case WIFI_IOT_GPIO_IDX_10: + func = IOT_GPIO_FUNC_GPIO_10_I2C0_SDA; + i2cId = IOT_I2C_IDX_0; + break; + case WIFI_IOT_GPIO_IDX_13: + func = IOT_GPIO_FUNC_GPIO_13_I2C0_SDA; + i2cId = IOT_I2C_IDX_0; + break; + case WIFI_IOT_GPIO_IDX_14: + func = IOT_GPIO_FUNC_GPIO_14_I2C0_SCL; + i2cId = IOT_I2C_IDX_0; + break; + default: + IOT_PRINTF_FUNC("Invalid gpio :%d \r\n", gpio); + return KP_ERR_FAILURE; + } + + return KP_ERR_SUCCESS; +} + + diff --git a/common/iot_gpio_interface/iot_gpio_func.h b/common/iot_gpio_interface/iot_gpio_func.h new file mode 100644 index 0000000000000000000000000000000000000000..dbe1d0cee41cbe39272c36256c7e96f6d47719a9 --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_func.h @@ -0,0 +1,25 @@ +#ifndef __IOT_GPIO_FUNC_H__ +#define __IOT_GPIO_FUNC_H__ + +#include "iot_base_type.h" + + +/* 通过GPIO引脚号获取GPIO功能编号 */ +u32 Gpio2Gpio(u32 gpio, u8 *func); + +/* 通过GPIO引脚号获取GPIO功能编号 */ +u32 Gpio2Uart(u32 gpio, u8 *func, u32 *uartId); + +/* 通过GPIO引脚号获取SPI功能编号 */ +u32 Gpio2Spi(u32 gpio, u8 *func, u32 *spiId); + +/* 通过GPIO引脚号获取PWM功能编号 */ +u32 Gpio2Pwm(u32 gpio, u8 *func, u32 *pwmId); + +/* 通过GPIO引脚号获取IIC功能编号 */ +u32 Gpio2IIC(u32 gpio, u8 *func, u32 *i2cId); + + + +#endif + diff --git a/common/iot_gpio_interface/iot_gpio_gpio.c b/common/iot_gpio_interface/iot_gpio_gpio.c new file mode 100644 index 0000000000000000000000000000000000000000..07ad7b8adf779037269778bbeae3e1e80670ce8c --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_gpio.c @@ -0,0 +1,69 @@ +#include "wifiiot_gpio.h" +#include "iot_gpio.h" +#include "iot_gpio_ex.h" +#include "iot_gpio_gpio.h" +#include "iot_gpio_func.h" + + +/* GPIO初始化复用为GPIO功能 */ +u32 GpioInitGpio(u32 pin) +{ + /* 根据输入参数GPIO引脚号初始化 */ + u32 ret = IoTGpioInit(pin); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioInit failed :%#x \r\n", ret); + return ret; + } + + /* 根据引脚号确定引脚GPIO功能配置值 */ + u8 pinFunc = 0; + ret = Gpio2Gpio(pin, &pinFunc); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("Gpio2Gpio failed :%#x \r\n", ret); + return ret; + } + + /* 设置复用为GPIO功能 */ + ret = IoTGpioSetFunc(pin, pinFunc); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioSetFunc failed :%#x \r\n", ret); + return ret; + } + + /* 设置成输出有效,通过输出高电平控制 */ + ret = IoTGpioSetDir(pin, IOT_GPIO_DIR_OUT); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioSetDir failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + +/* GPIO输出高低电平控制 */ +u32 GpioCtrlGpio(u32 pin, u32 state) +{ + u32 ret = KP_ERR_SUCCESS; + + switch (state) { + case GPIO_OUT_LOW: + case GPIO_OUT_HIGH: + { + /* 输出低电平控制 */ + ret = IoTGpioSetOutputVal(pin, state); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioSetOutputVal failed :%#x \r\n", ret); + return ret; + } + break; + } + default: + { + IOT_PRINTF_FUNC("Invalid state :%d \r\n", state); + return KP_ERR_FAILURE; + } + } + + return KP_ERR_SUCCESS; +} + diff --git a/common/iot_gpio_interface/iot_gpio_gpio.h b/common/iot_gpio_interface/iot_gpio_gpio.h new file mode 100644 index 0000000000000000000000000000000000000000..1c77cedfe94d44db012a211436dc7d7297737d91 --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_gpio.h @@ -0,0 +1,15 @@ +#ifndef __IOT_GPIO_GPIO_H__ +#define __IOT_GPIO_GPIO_H__ + +#include "iot_base_type.h" + + +/* GPIO初始化复用为GPIO */ +u32 GpioInitGpio(u32 pin); + +//通过输出高低电平控制GPIO +u32 GpioCtrlGpio(u32 pin, u32 state); + + +#endif + diff --git a/common/iot_gpio_interface/iot_gpio_i2c.c b/common/iot_gpio_interface/iot_gpio_i2c.c new file mode 100644 index 0000000000000000000000000000000000000000..d93e96c6b97add4b672c1eb2287ea517112eb513 --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_i2c.c @@ -0,0 +1,66 @@ +#include "wifiiot_gpio.h" +#include "iot_gpio.h" +#include "iot_gpio_ex.h" +#include "iot_i2c.h" +#include "iot_gpio_i2c.h" +#include "iot_gpio_func.h" + + +/* GPIO初始化复用为IIC功能 */ +u32 GpioInitIIC(u32 pinSDA, u32 pinSCL, u32 baud) +{ + /* 根据输入参数GPIO引脚号初始化 */ + u32 ret = IoTGpioInit(pinSDA); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioInit failed :%#x \r\n", ret); + return ret; + } + + /* 根据引脚号确定引脚IIC功能配置值及IIC端口号 */ + u8 sdaFunc = 0; + u32 i2cPort = 0; + ret = Gpio2IIC(pinSDA, &sdaFunc, &i2cPort); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("Gpio2IIC failed :%#x \r\n", ret); + return ret; + } + + /* 设置复用为IIC功能 */ + ret = IoTGpioSetFunc(pinSDA, sdaFunc); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioSetFunc failed :%#x \r\n", ret); + return ret; + } + + /* 根据引脚号确定引脚IIC功能配置值及IIC端口号 */ + u8 sclFunc = 0; + ret = Gpio2IIC(pinSCL, &sclFunc, &i2cPort); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("Gpio2IIC failed :%#x \r\n", ret); + return ret; + } + + /* 设置复用为IIC功能 */ + ret = IoTGpioSetFunc(pinSCL, sclFunc); + if(ret != 0){ + IOT_PRINTF_FUNC("IoTGpioSetFunc failed :%#x \r\n", ret); + return ret; + } + + /* IIC初始化 */ + ret = IoTI2cInit(i2cPort, baud); + if (ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("IoTI2cInit failed :%#x \r\n", ret); + return ret; + } + + /* IIC设置波特率 */ + ret = IoTI2cSetBaudrate(i2cPort, baud); + if (ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("IoTI2cSetBaudrate failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + diff --git a/common/iot_gpio_interface/iot_gpio_i2c.h b/common/iot_gpio_interface/iot_gpio_i2c.h new file mode 100644 index 0000000000000000000000000000000000000000..cdc7e77843e5f5fc4cecbc569b7415a6401f2243 --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_i2c.h @@ -0,0 +1,13 @@ +#ifndef __IOT_GPIO_IIC_H__ +#define __IOT_GPIO_IIC_H__ + +#include "iot_base_type.h" + + +/* GPIO初始化复用为IIC功能 */ +u32 GpioInitIIC(u32 pinSDA, u32 pinSCL, u32 baud); + + + +#endif + diff --git a/common/iot_gpio_interface/iot_gpio_pwm.c b/common/iot_gpio_interface/iot_gpio_pwm.c new file mode 100644 index 0000000000000000000000000000000000000000..a17732e594d1a15d2af2447a842dec7ca9928c45 --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_pwm.c @@ -0,0 +1,86 @@ +#include "wifiiot_gpio.h" +#include "iot_gpio.h" +#include "iot_gpio_ex.h" +#include "iot_pwm.h" +#include "iot_gpio_pwm.h" +#include "iot_gpio_func.h" + + +/* GPIO初始化复用为PWM功能 */ +u32 GpioInitPwm(u32 pin) +{ + /* 根据输入参数GPIO引脚号初始化 */ + u32 ret = IoTGpioInit(pin); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioInit failed :%#x \r\n", ret); + return ret; + } + + /* 根据引脚号确定引脚PWM功能配置值及PWM端口号 */ + u8 pwmFunc = 0; + u32 pwmPort = 0; + ret = Gpio2Pwm(pin, &pwmFunc, &pwmPort); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("Gpio2Pwm failed :%#x \r\n", ret); + return ret; + } + + /* 设置复用为PWM功能 */ + ret = IoTGpioSetFunc(pin, pwmFunc); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioSetFunc failed :%#x \r\n", ret); + return ret; + } + + /* PWM初始化 */ + ret = IoTPwmDeinit(pwmPort); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTPwmDeinit failed :%#x \r\n", ret); + return ret; + } + + ret = IoTPwmInit(pwmPort); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTPwmInit failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + +/* PWM控制 */ +u32 GpioCtrlPwm(u32 pwmPort, u32 state, u16 duty, u32 freq) +{ + u32 ret = KP_ERR_SUCCESS; + + switch (state) { + case PWM_OUT_START: + { + /* PWM控制启动 */ + ret = IoTPwmStart(pwmPort, duty, freq); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTPwmStart failed :%#x \r\n", ret); + return ret; + } + break; + } + case PWM_OUT_STOP: + { + /* PWM控制停止 */ + ret = IoTPwmStop(pwmPort); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTPwmStop failed :%#x \r\n", ret); + return ret; + } + break; + } + default: + { + IOT_PRINTF_FUNC("Invalid state :%d \r\n", state); + return KP_ERR_FAILURE; + } + } + + return KP_ERR_SUCCESS; +} + diff --git a/common/iot_gpio_interface/iot_gpio_pwm.h b/common/iot_gpio_interface/iot_gpio_pwm.h new file mode 100644 index 0000000000000000000000000000000000000000..41ae1dcc5e21089a701e1c25d7f7b272bb5297fb --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_pwm.h @@ -0,0 +1,16 @@ +#ifndef __IOT_GPIO_PWM_H__ +#define __IOT_GPIO_PWM_H__ + +#include "iot_base_type.h" + + +/* GPIO初始化复用为PWM功能 */ +u32 GpioInitPwm(u32 pin); + +/* PWM控制 */ +u32 GpioCtrlPwm(u32 pwmPort, u32 state, u16 duty, u32 freq); + + + +#endif + diff --git a/common/iot_gpio_interface/iot_gpio_spi.c b/common/iot_gpio_interface/iot_gpio_spi.c new file mode 100644 index 0000000000000000000000000000000000000000..1a0b78d324468b5e281d7053023ab75db89f94e3 --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_spi.c @@ -0,0 +1,83 @@ +#include "wifiiot_gpio.h" +#include "iot_gpio.h" +#include "iot_gpio_ex.h" +#include "iot_spi.h" +#include "iot_gpio_spi.h" +#include "iot_gpio_func.h" + + +/* GPIO初始化复用为SPI功能,pinRES为SPI复位引脚,pinRXD为SPI的数据/命令控制引脚,pinCS为SPI的片选引脚 */ +u32 GpioInitSpi(u32 pinRES, u32 pinRXD, u32 pinCS) +{ + /* 根据输入参数GPIO引脚号初始化 */ + u32 ret = KP_ERR_SUCCESS; + + // 连接SPI的RES引脚,负责复位SPI + ret = IoTGpioInit(pinRES); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTGpioInit failed :%#x \r\n", ret); + return ret; + } + + /* 根据引脚号确定引脚GPIO功能配置值 */ + u8 resFunc = 0; + ret = Gpio2Gpio(pinRES, &resFunc); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("Gpio2Gpio failed :%#x \r\n", ret); + return ret; + } + + /* 设置复用为GPIO功能 */ + ret = IoTGpioSetFunc(pinRES, resFunc); + if(ret != KP_ERR_SUCCESS){ + printf("IoTGpioSetFunc failed :%#x \r\n",ret); + return ret; + } + + /* 根据引脚号确定引脚SPI功能的指令控制DC端口号 */ + u8 rxdFunc = 0; + ret = Gpio2Gpio(pinRXD, &rxdFunc); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("Gpio2Gpio failed :%#x \r\n", ret); + return ret; + } + + /* 连接设备的DC指令控制引脚,负责对设备进行指令控制,设置复用为GPIO功能 */ + ret = IoTGpioSetFunc(pinRXD, rxdFunc); + if(ret != KP_ERR_SUCCESS){ + printf("IoTGpioSetFunc failed :%#x \r\n",ret); + return ret; + } + + /* 设置为输出控制 */ + ret = IoTGpioSetDir(pinRXD, IOT_GPIO_DIR_OUT); + if (ret != KP_ERR_SUCCESS) { + printf("IoTGpioSetDir failed :%#x \r\n",ret); + return; + } + + /* 根据引脚号确定引脚SPI功能的指令控制CS端口号 */ + u8 csFunc = 0; + ret = Gpio2Gpio(pinCS, &csFunc); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("Gpio2Gpio failed :%#x \r\n", ret); + return ret; + } + + //连接设备的CS片选引脚,负责使能设备,设置复用为GPIO功能 + ret = IoTGpioSetFunc(pinCS, csFunc); + if(ret != KP_ERR_SUCCESS){ + printf("IoTGpioSetFunc failed :%#x \r\n",ret); + return ret; + } + + /* 设置为输出控制 */ + ret = IoTGpioSetDir(pinCS, IOT_GPIO_DIR_OUT); + if (ret != KP_ERR_SUCCESS) { + printf("IoTGpioSetDir failed :%#x \r\n",ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + diff --git a/common/iot_gpio_interface/iot_gpio_spi.h b/common/iot_gpio_interface/iot_gpio_spi.h new file mode 100644 index 0000000000000000000000000000000000000000..17fce71cb48e933f14fa5b4b92ac6f0e0a349d6e --- /dev/null +++ b/common/iot_gpio_interface/iot_gpio_spi.h @@ -0,0 +1,31 @@ +#ifndef __SPI_SCREEN_H__ +#define __SPI_SCREEN_H__ + +#include +#include "iot_spi.h" +#include "iot_base_type.h" + + +#define test_spi_printf(fmt...) \ + do { \ + printf("[SPI TEST]" fmt); \ + printf("\n"); \ + } while (0) + +typedef struct { + hi_spi_cfg_basic_info cfg_info; + hi_spi_idx spi_id; + hi_u32 loop; + hi_u32 length; + hi_bool irq; + hi_bool slave; + hi_bool lb; + hi_bool dma_en; +} spi_para; + + +/* GPIO初始化复用为SPI功能,pinRES为SPI复位引脚,pinSPI为SPI控制引脚,可以是SPI四线中的任意一根 */ +u32 GpioInitSpi(u32 pinRES, u32 pinRXD, u32 pinCS); + + +#endif diff --git a/common/iot_led/BUILD.gn b/common/iot_led/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..aa79399ef03fe1c2992aa393eb7822d09a67c8cf --- /dev/null +++ b/common/iot_led/BUILD.gn @@ -0,0 +1,21 @@ + +static_library("iot_led") { + sources = [ + "led_gpio.c", + "led_pwm.c", + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "../../../iot_hardware_hals/include", + "../../iot_hardware_hals/include", + "../../include", + ] + + deps = [ ] + +} diff --git a/common/iot_led/BUILD.gn.bak b/common/iot_led/BUILD.gn.bak new file mode 100644 index 0000000000000000000000000000000000000000..6565797a3e1f2e3958be6116694e62cb99cf7360 --- /dev/null +++ b/common/iot_led/BUILD.gn.bak @@ -0,0 +1,19 @@ + +static_library("iot_led") { + sources = [ + "led_gpio.c", + "led_pwm.c", + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "../../../iot_hardware_hals/include", + ] + + deps = [ ] + +} diff --git a/common/iot_led/led_gpio.c b/common/iot_led/led_gpio.c new file mode 100644 index 0000000000000000000000000000000000000000..729297a81c81ad32184c1b35141b2b4890f21dff --- /dev/null +++ b/common/iot_led/led_gpio.c @@ -0,0 +1,126 @@ +#include "wifiiot_gpio.h" +#include "iot_gpio.h" +#include "iot_gpio_ex.h" +#include "led_gpio.h" + +static unsigned int spin = 0; + + +/* 初始化LED */ +u32 LedGpioInit(u32 pin) +{ + /* LED灯根据输入参数GPIO引脚初始化 */ + u32 ret = IoTGpioInit(pin); + if(ret != KP_ERR_SUCCESS){ + printf("IoTGpioInit failed :%#x \r\n",ret); + return ret; + } + + /* 保存引脚号 */ + spin = pin; + + /* 根据引脚号确定引脚GPIO功能配置值 */ + u8 pinFunc = 0; + switch (pin) + { + case WIFI_IOT_GPIO_IDX_0: + pinFunc = IOT_GPIO_FUNC_GPIO_0_GPIO; + break; + case WIFI_IOT_GPIO_IDX_1: + pinFunc = IOT_GPIO_FUNC_GPIO_1_GPIO; + break; + case WIFI_IOT_GPIO_IDX_2: + pinFunc = IOT_GPIO_FUNC_GPIO_2_GPIO; + break; + case WIFI_IOT_GPIO_IDX_3: + pinFunc = IOT_GPIO_FUNC_GPIO_3_GPIO; + break; + case WIFI_IOT_GPIO_IDX_4: + pinFunc = IOT_GPIO_FUNC_GPIO_4_GPIO; + break; + case WIFI_IOT_GPIO_IDX_5: + pinFunc = IOT_GPIO_FUNC_GPIO_5_GPIO; + break; + case WIFI_IOT_GPIO_IDX_6: + pinFunc = IOT_GPIO_FUNC_GPIO_6_GPIO; + break; + case WIFI_IOT_GPIO_IDX_7: + pinFunc = IOT_GPIO_FUNC_GPIO_7_GPIO; + break; + case WIFI_IOT_GPIO_IDX_8: + pinFunc = IOT_GPIO_FUNC_GPIO_8_GPIO; + break; + case WIFI_IOT_GPIO_IDX_9: + pinFunc = IOT_GPIO_FUNC_GPIO_9_GPIO; + break; + case WIFI_IOT_GPIO_IDX_10: + pinFunc = IOT_GPIO_FUNC_GPIO_10_GPIO; + break; + case WIFI_IOT_GPIO_IDX_11: + pinFunc = IOT_GPIO_FUNC_GPIO_11_GPIO; + break; + case WIFI_IOT_GPIO_IDX_12: + pinFunc = IOT_GPIO_FUNC_GPIO_12_GPIO; + break; + case WIFI_IOT_GPIO_IDX_13: + pinFunc = IOT_GPIO_FUNC_GPIO_13_GPIO; + break; + case WIFI_IOT_GPIO_IDX_14: + pinFunc = IOT_GPIO_FUNC_GPIO_14_GPIO; + break; + default: + return KP_ERR_FAILURE; + } + + /* 设置复用为GPIO功能 */ + ret = IoTGpioSetFunc(pin, pinFunc); + if(ret != KP_ERR_SUCCESS){ + printf("IoTGpioSetFunc failed :%#x \r\n", ret); + return ret; + } + + /* 设置成输出有效,通过输出高电平点亮LED灯 */ + ret = IoTGpioSetDir(pin, IOT_GPIO_DIR_OUT); + if(ret != KP_ERR_SUCCESS){ + printf("IoTGpioSetDir failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + +/* 点亮或熄灭LED灯 */ +void LedGpioCtrl(u32 state) +{ + switch (state){ + case LED_OFF: + { + /* LED灯熄灭 */ + IoTGpioSetOutputVal(spin, GPIO_OUT_LOW); + printf("----- safeguard alarm led turn off -----\r\n"); + break; + } + case LED_ON: + { + /* LED灯点亮 */ + IoTGpioSetOutputVal(spin, GPIO_OUT_HIGH); + printf("----- safeguard alarm led turn on -----\r\n"); + break; + } + case LED_SPARK: + { + /* LED灯闪烁 */ + IoTGpioSetOutputVal(spin, GPIO_OUT_HIGH); + osDelay(LED_SPARK_INTERVAL_TIME); + IoTGpioSetOutputVal(spin, GPIO_OUT_LOW); + osDelay(LED_SPARK_INTERVAL_TIME); + IoTGpioSetOutputVal(spin, GPIO_OUT_HIGH); + osDelay(LED_SPARK_INTERVAL_TIME); + IoTGpioSetOutputVal(spin, GPIO_OUT_LOW); + break; + } + default: + break; + } +} + diff --git a/common/iot_led/led_gpio.h b/common/iot_led/led_gpio.h new file mode 100644 index 0000000000000000000000000000000000000000..a258518316987bdb515de56eefb7cfe0bc22a2f9 --- /dev/null +++ b/common/iot_led/led_gpio.h @@ -0,0 +1,15 @@ +#ifndef __LED_GPIO_H__ +#define __LED_GPIO_H__ + +#include "iot_base_type.h" + + +//初始化烟雾探测模块的LED报警灯 +void SafeguardLedInit(void); + +//控制烟雾探测模块的LED报警灯,1:点亮; 2:熄灭 +void SafeguardLedCtrl(u32 state); + + +#endif + diff --git a/common/iot_led/led_pwm.c b/common/iot_led/led_pwm.c new file mode 100644 index 0000000000000000000000000000000000000000..40c0008051e58fae23b936b1c7d084de6b5f72eb --- /dev/null +++ b/common/iot_led/led_pwm.c @@ -0,0 +1,137 @@ +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "iot_pwm.h" +#include "wifiiot_gpio.h" +#include "iot_gpio.h" +#include "iot_gpio_ex.h" +#include "led_pwm.h" + +#define LED_FREQ 64000 +#define LED_DUTY 100 + +static unsigned int spwmPort = 0; + + +/* 初始化LED模块 */ +u32 LedPwmInit(u32 pin) +{ + /* LED灯根据输入参数GPIO引脚初始化 */ + u32 ret = IoTGpioInit(pin); + if(ret != 0){ + printf("IoTGpioInit failed :%#x \r\n",ret); + return ret; + } + + /* 根据引脚号确定引脚PWM功能配置值及PWM端口号 */ + u8 pinFunc = 0; + switch (pin) + { + case WIFI_IOT_GPIO_IDX_0: + pinFunc = IOT_GPIO_FUNC_GPIO_0_PWM3_OUT; + spwmPort = PWM_PORT_PWM3; + break; + case WIFI_IOT_GPIO_IDX_1: + pinFunc = IOT_GPIO_FUNC_GPIO_1_PWM4_OUT; + spwmPort = PWM_PORT_PWM4; + break; + case WIFI_IOT_GPIO_IDX_2: + pinFunc = IOT_GPIO_FUNC_GPIO_2_PWM2_OUT; + spwmPort = PWM_PORT_PWM2; + break; + case WIFI_IOT_GPIO_IDX_3: + pinFunc = IOT_GPIO_FUNC_GPIO_3_PWM5_OUT; + spwmPort = PWM_PORT_PWM5; + break; + case WIFI_IOT_GPIO_IDX_4: + pinFunc = IOT_GPIO_FUNC_GPIO_4_PWM1_OUT; + spwmPort = PWM_PORT_PWM1; + break; + case WIFI_IOT_GPIO_IDX_5: + pinFunc = IOT_GPIO_FUNC_GPIO_5_PWM2_OUT; + spwmPort = PWM_PORT_PWM2; + break; + case WIFI_IOT_GPIO_IDX_6: + pinFunc = IOT_GPIO_FUNC_GPIO_6_PWM3_OUT; + spwmPort = PWM_PORT_PWM3; + break; + case WIFI_IOT_GPIO_IDX_7: + pinFunc = IOT_GPIO_FUNC_GPIO_7_PWM0_OUT; + spwmPort = PWM_PORT_PWM0; + break; + case WIFI_IOT_GPIO_IDX_8: + pinFunc = IOT_GPIO_FUNC_GPIO_8_PWM1_OUT; + spwmPort = PWM_PORT_PWM1; + break; + case WIFI_IOT_GPIO_IDX_9: + pinFunc = IOT_GPIO_FUNC_GPIO_9_PWM0_OUT; + spwmPort = PWM_PORT_PWM0; + break; + case WIFI_IOT_GPIO_IDX_10: + pinFunc = IOT_GPIO_FUNC_GPIO_10_PWM1_OUT; + spwmPort = PWM_PORT_PWM1; + break; + case WIFI_IOT_GPIO_IDX_11: + pinFunc = IOT_GPIO_FUNC_GPIO_11_PWM2_OUT; + spwmPort = PWM_PORT_PWM2; + break; + case WIFI_IOT_GPIO_IDX_12: + pinFunc = IOT_GPIO_FUNC_GPIO_12_PWM3_OUT; + spwmPort = PWM_PORT_PWM3; + break; + case WIFI_IOT_GPIO_IDX_13: + pinFunc = IOT_GPIO_FUNC_GPIO_13_PWM4_OUT; + spwmPort = PWM_PORT_PWM4; + break; + case WIFI_IOT_GPIO_IDX_14: + pinFunc = IOT_GPIO_FUNC_GPIO_14_PWM5_OUT; + spwmPort = PWM_PORT_PWM5; + break; + default: + return KP_ERR_FAILURE; + } + + /* 设置复用为PWM功能 */ + ret = IoTGpioSetFunc(pin, pinFunc); + if(ret != 0){ + printf("IoTGpioSetFunc failed :%#x \r\n",ret); + return ret; + } + + /* PWM初始化 */ + ret = IoTPwmDeinit(spwmPort); + if(ret != 0){ + printf("IoTPwmDeinit failed :%#x \r\n",ret); + return ret; + } + + ret = IoTPwmInit(spwmPort); + if(ret != 0){ + printf("IoTPwmInit failed :%#x \r\n",ret); + return ret; + } +} + +void LedPwmCtrl(unsigned int status) +{ + switch (status) { + case LED_ON: + IoTPwmStart(spwmPort, LED_DUTY, LED_FREQ); + break; + case LED_OFF: + IoTPwmStop(spwmPort); + break; + case LED_SPARK: + IoTPwmStart(spwmPort, LED_DUTY, LED_FREQ); + osDelay(LED_SPARK_INTERVAL_TIME); + IoTPwmStop(spwmPort); + osDelay(LED_SPARK_INTERVAL_TIME); + break; + default: + osDelay(LED_SPARK_INTERVAL_TIME); + break; + } + + return; +} + diff --git a/common/iot_led/led_pwm.h b/common/iot_led/led_pwm.h new file mode 100644 index 0000000000000000000000000000000000000000..7b7a3dc8b3d8afdb434b82a4bcf53425940f023d --- /dev/null +++ b/common/iot_led/led_pwm.h @@ -0,0 +1,11 @@ +#ifndef __LED_PWM_H__ +#define __LED_PWM_H__ + +#include "iot_base_type.h" + +/* 初始化LED模块 */ +u32 LedPwmInit(u32 pin); +void LedPwmCtrl(unsigned int status); + +#endif + diff --git a/common/iot_list/BUILD.gn b/common/iot_list/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a0e9df620e215d1ec3fc2eccd242b6f71a6f9982 --- /dev/null +++ b/common/iot_list/BUILD.gn @@ -0,0 +1,32 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +source_set("iot_list") { + sources = [ + "iot_list.c", + "iot_store_manager.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "../inc" + ] + + deps = [ + + ] + +} \ No newline at end of file diff --git a/common/iot_list/iot_list.c b/common/iot_list/iot_list.c new file mode 100644 index 0000000000000000000000000000000000000000..a3d60399e2c18af84d070142a5e95d04cfdf83e3 --- /dev/null +++ b/common/iot_list/iot_list.c @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "iot_list.h" + +#define LIST_DEBUG +#ifdef LIST_DEBUG +#define LST_DBG(fmt, args...) printf("[LIST_DEBUG][%s|%d]" fmt, __func__, __LINE__, ##args) +#define LST_ERR(fmt, args...) printf("[LIST_ERROR][%s|%d]" fmt, __func__, __LINE__, ##args) +#else +#define LST_DBG(fmt, args...) do {} while (0) +#define LST_ERR(fmt, args...) do {} while (0) +#endif + +typedef struct __ListNode { + void *data; + int size; + struct __ListNode *prev; + struct __ListNode *next; +} ListNode; + +typedef struct { + ListNode *head, *tail; + int total; +} DLinkList; + +static ListNode *ListNodeCreate(void *data, int size) +{ + ListNode *pNode = NULL; + if (data == NULL || size <= 0) { + LST_ERR("NULL POINT!\n"); + return pNode; + } + + pNode = (ListNode *)malloc(sizeof(ListNode)); + if (pNode == NULL) { + LST_ERR("OOM!\n"); + return NULL; + } + + pNode->data = malloc(size); + if (pNode->data == NULL) { + LST_ERR("OOM!\n"); + free(pNode); + return NULL; + } + + memcpy(pNode->data, data, size); + pNode->size = size; + pNode->prev = NULL; + pNode->next = NULL; + + return pNode; +} + +static void ListNodeDestroy(ListNode *pNode) +{ + if (pNode == NULL) { + return; + } + if (pNode->data) { + free(pNode->data); + pNode->data = NULL; + } + free(pNode); +} + +static ListNode *ListNodeSearch(ListNode *head, int idx) +{ + ListNode *pNode = head; + for (int i = 0; i < idx; i++) { + if (pNode == NULL) { + break; + } + pNode = pNode->next; + } + + return pNode; +} + +static ListNode *ListNodeSearchData(ListNode *head, void *data) +{ + ListNode *pNode = head; + + while (pNode != NULL) { + if (memcmp(pNode->data, data, pNode->size) == 0) { + break; + } + pNode = pNode->next; + } + + return pNode; +} + +IOT_LIST IoT_ListCreate(void *data, int size) +{ + ListNode *pNode = NULL; + DLinkList *list = (DLinkList *)malloc(sizeof(DLinkList)); + if (list == NULL) { + LST_ERR("OOM!\n"); + return NULL; + } + + pNode = ListNodeCreate(data, size); + if (pNode != NULL) { + list->total = 1; + } else { + list->total = 0; + } + list->head = list->tail = pNode; + + return (DLinkList *)list; +} + +int IoT_ListAppend(IOT_LIST mHandle, void *data, int size) +{ + ListNode *pNode = NULL; + DLinkList *list = (DLinkList *)mHandle; + if (list == NULL) { + LST_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ListNodeCreate(data, size); + if (pNode == NULL) { + LST_ERR("ListNodeCreate failed! \n"); + return -1; + } + + if (list->head == NULL) { + list->head = list->tail = pNode; + } else { + list->tail->next = pNode; + pNode->prev = list->tail; + list->tail = pNode; + } + + list->total++; + + return 0; +} + +int IoT_ListUpdate(IOT_LIST mHandle, int idx, void *data, int size) +{ + int retval = 0; + ListNode *pNode = NULL; + DLinkList *list = (DLinkList *)mHandle; + if (list == NULL || data == NULL) { + LST_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ListNodeSearch(list->head, idx); + if (pNode != NULL) { + if (size != pNode->size) { + pNode->data = realloc(pNode->data, size); + pNode->size = size; + } + memcpy(pNode->data, data, size); + } else { + retval = IoT_ListAppend(mHandle, data, size); + } + + return retval; +} + +int IoT_ListDelete(IOT_LIST mHandle, void *data) +{ + ListNode *pNode = NULL; + ListNode *prev = NULL; + ListNode *next = NULL; + DLinkList *list = (DLinkList *)mHandle; + if (list == NULL || data == NULL) { + LST_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ListNodeSearchData(list->head, data); + if (pNode == NULL) { + return -1; + } + prev = pNode->prev; + next = pNode->next; + + if (prev != NULL && next != NULL){ + prev->next = next; + next->prev = prev; + } else if (prev == NULL && next != NULL) { + next->prev = NULL; + list->head = next; + } else if (prev != NULL && next == NULL) { + prev->next = NULL; + list->tail = prev; + } else { + list->head = list->tail = NULL; + } + pNode->next = NULL; + pNode->prev = NULL; + list->total--; + + ListNodeDestroy(pNode); + pNode = NULL; + + return 0; +} + +int IoT_ListGet(IOT_LIST mHandle, int idx, void *data, int size) +{ + ListNode *pNode; + DLinkList *list = (DLinkList *)mHandle; + if (list == NULL) { + LST_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ListNodeSearch(list->head, idx); + if (pNode == NULL || pNode->data == NULL || data == NULL || size < pNode->size) { + LST_ERR("no such node(%d) \n", idx); + return -1; + } + + memcpy(data, pNode->data, pNode->size); + + return 0; +} + +int IoT_ListGetSize(IOT_LIST mHandle) +{ + if (mHandle == NULL) { + return -1; + } + + return ((DLinkList *)mHandle)->total; +} + +void IoT_ListClear(IOT_LIST mHandle) +{ + DLinkList *list = (DLinkList *)mHandle; + if (list == NULL) { + return; + } + while (list->head) { + ListNode *pNode = list->head; + list->head = pNode->next; + pNode->next = NULL; + list->head->prev = NULL; + + ListNodeDestroy(pNode); + } + list->total = 0; +} + +void IoT_ListDestroy(IOT_LIST mHandle) +{ + DLinkList *list = (DLinkList *)mHandle; + if (list == NULL) { + return; + } + while (list->head) { + ListNode *pNode = list->head; + list->head = pNode->next; + pNode->next = NULL; + list->head->prev = NULL; + + ListNodeDestroy(pNode); + } + list->total = 0; + + free(list); + list = NULL; +} diff --git a/common/iot_list/iot_store_manager.c b/common/iot_list/iot_store_manager.c new file mode 100644 index 0000000000000000000000000000000000000000..110511ce7b6c76948cf7e47c13cb6d07026a07c3 --- /dev/null +++ b/common/iot_list/iot_store_manager.c @@ -0,0 +1,530 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "ohos_errno.h" +#include "ohos_types.h" +#include "utils_config.h" +#include "utils_file.h" + +#include "iot_store_manager.h" + +#if (USER_KV_STORE) +#define INDX_KEY "//offset.idx" +#define INDX_TMP "//offset.tmp" +#define DATA_KEY "//data.info" +#define DATA_TMP "//data.tmp" + +#define TEMP_KEY "//tmp.idx" + +#define STORE_DEBUG +#ifdef STORE_DEBUG +#define STORE_DBG(fmt, args...) printf("[%s|%d][STORE_DEBUG]" fmt, __func__, __LINE__, ##args) +#define STORE_ERR(fmt, args...) printf("[%s|%d][STORE_ERROR]" fmt, __func__, __LINE__, ##args) +#else +#define STORE_DBG(fmt, args...) do {} while (0) +#define STORE_ERR(fmt, args...) do {} while (0) +#endif + +#define UTILS_CLOSE(fdx, idx) ({ \ + if ((fdx) != -1) { \ + close((fdx)); \ + (fdx) = -1; \ + } \ + if ((idx) != -1) { \ + close((idx)); \ + (idx) = -1; \ + } \ + }) + +static int GetFileSize(const char *filename) +{ + int offset = 0; + struct stat fileStat; + if (stat(filename, &fileStat) == 0) { + offset = fileStat.st_size; + } + + return offset; +} + +int StoreManagerAddData(const char *data, int length) +{ + int total = 0; + int offset = 0; + int ret = 0; + int fd = -1; + int fidx = -1; + + offset = GetFileSize(DATA_KEY); + + if (data == NULL || length <= 0) { + STORE_ERR("NULL POINT! \n"); + return -1; + } + + fidx = open(INDX_KEY, O_RDWR | O_CREAT | O_APPEND, 0); + if (fidx < 0) { + STORE_ERR("UtilsFileOpen %s failed! \n", INDX_KEY); + return -1; + } + + fd = open(DATA_KEY, O_RDWR | O_CREAT | O_APPEND, 0); + if (fd < 0) { + ret = -1; + STORE_ERR("UtilsFileOpen %s failed! \n", DATA_KEY); + goto ADD_EXIT; + } + + lseek(fidx, 0, SEEK_SET); + + if (read(fidx, &total, sizeof(int)) < 0) { + ret = -1; + STORE_ERR("UtilsFileRead total failed! \n"); + goto ADD_EXIT; + } + total += 1; + printf("save total = %d! \n", total); + + lseek(fidx, 0, SEEK_SET); + + if (write(fidx, &total, sizeof(int)) < 0) { + ret = -1; + STORE_ERR("UtilsFileWrite total failed! \n"); + goto ADD_EXIT; + } + lseek(fidx, 0, SEEK_END); + + if (write(fidx, &offset, sizeof(int)) < 0) { + ret = -1; + STORE_ERR("UtilsFileWrite offset failed! \n"); + goto ADD_EXIT; + } + lseek(fd, 0, SEEK_END); + write(fd, &length, sizeof(int)); + + if (write(fd, data, length) < 0) { + ret = -1; + STORE_ERR("UtilsFileWrite data failed! \n"); + goto ADD_EXIT; + } + STORE_DBG("save data[%d|%s]->%d success! \n", length, data, offset); +ADD_EXIT: + + UTILS_CLOSE(fd, fidx); + + return ret; +} + +int StoreManagerUpdateData(int idx, const char *data, int length) +{ + int fd = -1; + int fidx = -1; + int tmp = -1; + int tmpidx = -1; + int total = 0; + + fidx = open(INDX_KEY, O_RDWR, 0); + if (fidx < 0) { + printf("UtilsFileOpen INDX_KEY failed! \n"); + return -1; + } + + lseek(fidx, 0, SEEK_SET); + if (read(fidx, (char *)&total, sizeof(int)) < 0) { + printf("UtilsFileRead failed! \n"); + goto ERR_UPDATE; + } + if (total <= 0) { + printf("total is 0! \n"); + goto ERR_UPDATE; + } + + if (idx >= total) { + printf("idx(%d) > total(%d) \n", idx, total); + goto ERR_UPDATE; + } + + fd = open(DATA_KEY, O_RDWR, 0); + if (fd < 0) { + printf("UtilsFileOpen DATA_KEY failed! \n"); + goto ERR_UPDATE; + } + + tmp = open(DATA_TMP, O_CREAT | O_RDWR, 0); + if (tmp < 0) { + printf("UtilsFileOpen DATA_TMP failed! \n"); + goto ERR_UPDATE; + } + tmpidx = open(INDX_TMP, O_CREAT | O_RDWR, 0); + if (tmpidx < 0) { + printf("UtilsFileOpen INDX_TMP failed! \n"); + goto ERR_UPDATE; + } + + if (write(tmpidx, &total, sizeof(int)) < 0) { + printf("UtilsFileWrite tmpidx failed! \n"); + goto ERR_UPDATE; + } + + for (int i = 0; i <= total; i++) { + void *pdata = NULL; + int offset = 0; + int psize = 0; + + if (read(fidx, (void *)&offset, sizeof(int)) < 0) { + printf("UtilsFileRead fidx offset failed! \n"); + goto ERR_UPDATE; + } + if (i == idx) { + offset = GetFileSize(DATA_TMP); + write(tmp, data, length); + write(tmpidx, (void *)&offset, sizeof(int)); + continue; + } + + lseek(fd, offset, SEEK_SET); + if (read(fd, &psize, sizeof(int)) < 0) { + printf("UtilsFileRead fd psize failed! \n"); + goto ERR_UPDATE; + } + if (psize <= 0) { + goto ERR_UPDATE; + } + pdata = malloc(psize); + if (read(fd, pdata, psize) < 0) { + free(pdata); + goto ERR_UPDATE; + } + offset = GetFileSize(DATA_TMP); + write(tmp, psize, sizeof(int)); + write(tmp, pdata, psize); + write(tmpidx, &offset, sizeof(int)); + + free(pdata); + } + + UTILS_CLOSE(fd, fidx); + UTILS_CLOSE(tmp, tmpidx); + + unlink(INDX_KEY); + unlink(DATA_KEY); + + rename(DATA_TMP, DATA_KEY); + rename(INDX_TMP, INDX_KEY); + + return 0; +ERR_UPDATE: + + UTILS_CLOSE(fd, fidx); + UTILS_CLOSE(tmp, tmpidx); + + return -1; +} + +int StoreManagerDeleteDataWithId(int idx) +{ + int fd = -1; + int fidx = -1; + int tmp = -1; + int tmpidx = -1; + int total = 0; + + fidx = open(INDX_KEY, O_RDWR, 0); + if (fidx < 0) { + return -1; + } + + lseek(fidx, 0, SEEK_SET); + if (read(fidx, &total, sizeof(int)) < 0) { + goto ERR_DELETE_ID; + } + if (total <= 0) { + goto ERR_DELETE_ID; + } + + if (idx >= total) { + goto ERR_DELETE_ID; + } + + if (total == 1) { + close(fidx); + unlink(INDX_KEY); + unlink(DATA_KEY); + return 0; + } + + total -= 1; + fd = open(DATA_KEY, O_RDWR, 0); + if (fd < 0) { + goto ERR_DELETE_ID; + } + + tmp = open(DATA_TMP, O_CREAT | O_RDWR, 0); + if (tmp < 0) { + goto ERR_DELETE_ID; + } + tmpidx = open(INDX_TMP, O_CREAT | O_RDWR, 0); + if (tmpidx < 0) { + goto ERR_DELETE_ID; + } + + if (write(tmpidx, &total, sizeof(int)) < 0) { + goto ERR_DELETE_ID; + } + + for (int i = 0; i <= total; i++) { + void *data = NULL; + int offset = 0; + int size = 0; + + if (read(fidx, &offset, sizeof(int)) < 0) { + goto ERR_DELETE_ID; + } + if (i == idx) { + continue; + } + + lseek(fd, offset, SEEK_SET); + if (read(fd, &size, sizeof(int)) < 0) { + goto ERR_DELETE_ID; + } + if (size <= 0) { + goto ERR_DELETE_ID; + } + data = malloc(size); + if (read(fd, data, size) < 0) { + free(data); + goto ERR_DELETE_ID; + } + offset = GetFileSize(DATA_TMP); + write(tmp, size, sizeof(int)); + write(tmp, data, size); + write(tmpidx, &offset, sizeof(int)); + + free(data); + } + + UTILS_CLOSE(fd, fidx); + UTILS_CLOSE(tmp, tmpidx); + + unlink(INDX_KEY); + unlink(DATA_KEY); + + rename(DATA_TMP, DATA_KEY); + rename(INDX_TMP, INDX_KEY); + + return 0; + +ERR_DELETE_ID: + + UTILS_CLOSE(fd, fidx); + UTILS_CLOSE(tmp, tmpidx); + + return -1; +} + +int StoreManagerDeleteData(const char *data) +{ + int fd = -1; + int fidx = -1; + int tmp = -1; + int tmpidx = -1; + int total = 0; + + fidx = open(INDX_KEY, O_RDWR, 0); + if (fidx < 0) { + return -1; + } + + lseek(fidx, 0, SEEK_SET); + if (read(fidx, &total, sizeof(int)) < 0) { + goto ERR_DELETE_DATA; + } + if (total <= 0) { + goto ERR_DELETE_DATA; + } + + fd = open(DATA_KEY, O_RDWR, 0); + if (fd < 0) { + goto ERR_DELETE_DATA; + } + + tmp = open(DATA_TMP, O_CREAT | O_RDWR, 0); + if (tmp < 0) { + goto ERR_DELETE_DATA; + } + tmpidx = open(INDX_TMP, O_CREAT | O_RDWR, 0); + if (tmpidx < 0) { + goto ERR_DELETE_DATA; + } + + if (write(tmpidx, &total, sizeof(int)) < 0) { + goto ERR_DELETE_DATA; + } + + for (int i = 0; i <= total; i++) { + void *pdata = NULL; + int offset = 0; + int psize = 0; + + if (read(fidx, &offset, sizeof(int)) < 0) { + goto ERR_DELETE_DATA; + } + + lseek(fd, offset, SEEK_SET); + if (read(fd, &psize, sizeof(int)) < 0) { + goto ERR_DELETE_DATA; + } + if (psize <= 0) { + goto ERR_DELETE_DATA; + } + pdata = malloc(psize); + if (read(fd, pdata, psize) < 0) { + free(pdata); + goto ERR_DELETE_DATA; + } + + if (memcmp(pdata, data, psize) != 0) { + offset = GetFileSize(DATA_TMP); + write(tmp, psize, sizeof(int)); + write(tmp, pdata, psize); + write(tmpidx, &offset, sizeof(int)); + } + + free(pdata); + } + + UTILS_CLOSE(fd, fidx); + UTILS_CLOSE(tmp, tmpidx); + + unlink(INDX_KEY); + unlink(DATA_KEY); + + rename(DATA_TMP, DATA_KEY); + rename(INDX_TMP, INDX_KEY); + + return 0; +ERR_DELETE_DATA: + + UTILS_CLOSE(fd, fidx); + UTILS_CLOSE(tmp, tmpidx); + + return -1; +} + +int StoreManagerGetTotal(void) +{ + int total = 0; + int fd = open(INDX_KEY, O_RDWR, 0); + + if (fd < 0) { + STORE_ERR("open INDX_KEY failed! \n"); + return -1; + } + + lseek(fd, 0, SEEK_SET); + + if (read(fd, &total, sizeof(int)) < 0) { + STORE_ERR("read total failed! \n"); + total = -1; + } else { + STORE_ERR("read total =%d \n", total); + } + + close(fd); + + return total; +} + +int StoreManagerGetData(char *data, int size, int idx) +{ + int fd = -1; + int fidx = -1; + unsigned int total = 0; + unsigned int offset = 0; + unsigned int psize = 0; + + fidx = open(INDX_KEY, O_RDWR, 0); + if (fidx < 0) { + return -1; + } + + fd = open(DATA_KEY, O_RDWR, 0); + if (fd < 0) { + UTILS_CLOSE(fd, fidx); + return -1; + } + lseek(fidx, 0, SEEK_SET); + + if (read(fidx, &total, sizeof(int)) < 0) { + UTILS_CLOSE(fd, fidx); + return -1; + } + + if (idx >= total) { + UTILS_CLOSE(fd, fidx); + return -1; + } + + if (lseek(fidx, (idx + 1) * sizeof(int), SEEK_SET) < 0) { + UTILS_CLOSE(fd, fidx); + return -1; + } + + if (read(fidx, &offset, sizeof(int)) < 0) { + UTILS_CLOSE(fd, fidx); + return -1; + } + + if (lseek(fd, offset, SEEK_SET) < 0) { + UTILS_CLOSE(fd, fidx); + return -1; + } + + if (read(fd, &psize, sizeof(int)) < 0) { + UTILS_CLOSE(fd, fidx); + return -1; + } + + if (size < psize) { + UTILS_CLOSE(fd, fidx); + return -1; + } + + if (read(fd, data, psize) < 0) { + UTILS_CLOSE(fd, fidx); + return -1; + } + + UTILS_CLOSE(fd, fidx); + + return 0; +} + +void StoreManagerDelete(void) +{ + unlink(DATA_KEY); + unlink(INDX_KEY); +} + +#endif diff --git a/common/iot_nfc/BUILD.gn b/common/iot_nfc/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a056f96d2d74f801c77cb17e862288675855e45f --- /dev/null +++ b/common/iot_nfc/BUILD.gn @@ -0,0 +1,41 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_nfc") { + sources = [ + "NT3H.c", + "nfc.c", + "nfc_app.c", + "ndef/rtd/nfcForum.c", + "ndef/rtd/rtdText.c", + "ndef/rtd/rtdUri.c", + "ndef/ndef.c", + + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/iot_hardware/peripheral/interfaces/kits", + "//third_party/cJSON", + ".", + "ndef", + "ndef/rtd/", + "../inc", + ] + + deps = [ ] + +} \ No newline at end of file diff --git a/common/iot_nfc/NT3H.c b/common/iot_nfc/NT3H.c new file mode 100644 index 0000000000000000000000000000000000000000..7892f01d4186b3f8d25721c99be5cfeaf75acd85 --- /dev/null +++ b/common/iot_nfc/NT3H.c @@ -0,0 +1,217 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "iot_i2c.h" +#include "iot_i2c_ex.h" +#include "NT3H.h" +#include "ndef.h" +#include "nfc.h" +#include "nfcForum.h" + +/** + * @brief Defines I2C data transmission attributes. + */ +typedef struct { + /** Pointer to the buffer storing data to send */ + unsigned char *sendBuf; + /** Length of data to send */ + unsigned int sendLen; + /** Pointer to the buffer for storing data to receive */ + unsigned char *receiveBuf; + /** Length of data received */ + unsigned int receiveLen; +} WifiIotI2cData; + +uint8_t nfcPageBuffer[NFC_PAGE_SIZE]; +NT3HerrNo errNo; +// due to the nature of the NT3H a timeout is required to +// protectd 2 consecutive I2C access + +inline const uint8_t* get_last_ncf_page(void) { + return nfcPageBuffer; +} + +static bool writeTimeout( uint8_t *data, uint8_t dataSend) { + uint32_t status = 0; + status = IoTI2cWrite(1, (NT3H1X_SLAVE_ADDRESS<<1)|0x00, data,dataSend); + if (status != 0) { + printf("===== Error: I2C write status1 = 0x%x! =====\r\n", status); + return 0; + } + usleep(300000); + return 1; +} + +static bool readTimeout(uint8_t address, uint8_t *block_data) { + uint32_t status = 0; + IotI2cData nt3h1101_i2c_data = {0}; + uint8_t buffer[1] = {address}; + nt3h1101_i2c_data.sendBuf = buffer; + nt3h1101_i2c_data.sendLen = 1; + nt3h1101_i2c_data.receiveBuf = block_data; + nt3h1101_i2c_data.receiveLen = NFC_PAGE_SIZE; + status = IoTI2cWriteread(1, (NT3H1X_SLAVE_ADDRESS<<1)|0x00, &nt3h1101_i2c_data); + if (status != 0) { + printf("===== Error: I2C write status = 0x%x! =====\r\n", status); + return 0; + } + return 1; +} + +bool NT3HReadHeaderNfc(uint8_t *endRecordsPtr, uint8_t *ndefHeader) { + *endRecordsPtr=0; + bool ret = NT3HReadUserData(0); + + // read the first page to see where is the end of the Records. + if (ret == true) { + // if the first byte is equals to NDEF_START_BYTE there are some records + // store theend of that + if ((NDEF_START_BYTE == nfcPageBuffer[0]) && (NTAG_ERASED != nfcPageBuffer[2])) { + *endRecordsPtr = nfcPageBuffer[1]; + *ndefHeader = nfcPageBuffer[2]; + } + return true; + } else { + errNo = NT3HERROR_READ_HEADER; + } + + return ret; +} + + +bool NT3HWriteHeaderNfc(uint8_t endRecordsPtr, uint8_t ndefHeader) { + + // read the first page to see where is the end of the Records. + bool ret = NT3HReadUserData(0); + if (ret == true) { + + nfcPageBuffer[1] = endRecordsPtr; + nfcPageBuffer[2] = ndefHeader; + ret = NT3HWriteUserData(0, nfcPageBuffer); + if (ret == false) { + errNo = NT3HERROR_WRITE_HEADER; + } + } else { + errNo = NT3HERROR_READ_HEADER; + } + + return ret; +} + + + +bool NT3HEraseAllTag(void) { + bool ret = true; + uint8_t erase[NFC_PAGE_SIZE+1] = {USER_START_REG, 0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE}; + ret = writeTimeout(erase, sizeof(erase)); + + if (ret == false) { + errNo = NT3HERROR_ERASE_USER_MEMORY_PAGE; + } + return ret; +} + +bool NT3HReaddManufactoringData(uint8_t *manuf) { + return readTimeout(MANUFACTORING_DATA_REG, manuf); +} + +bool NT3HReadConfiguration(uint8_t *configuration){ + return readTimeout(CONFIG_REG, configuration); +} + +bool getSessionReg(void) { + return readTimeout(SESSION_REG, nfcPageBuffer); +} + + +bool NT3HReadUserData(uint8_t page) { + uint8_t reg = USER_START_REG+page; + // if the requested page is out of the register exit with error + if (reg > USER_END_REG) { + errNo = NT3HERROR_INVALID_USER_MEMORY_PAGE; + return false; + } + + bool ret = readTimeout(reg, nfcPageBuffer); + + if (ret == false) { + errNo = NT3HERROR_READ_USER_MEMORY_PAGE; + } + + return ret; +} + +void NT3H1101_Read_Userpages(uint8_t pagenumber,uint8_t *outbuf) +{ + for (int i = 0; i < pagenumber; i++) + { + NT3HReadUserData(i); + for (int j = 0; j < 16; j++) + { + outbuf[16 * i + j] = nfcPageBuffer[j]; + } + } +} + +bool NT3HWriteUserData(uint8_t page, const uint8_t* data) { + bool ret = true; + uint8_t dataSend[NFC_PAGE_SIZE +1]; // data plus register + uint8_t reg = USER_START_REG+page; + + // if the requested page is out of the register exit with error + if (reg > USER_END_REG) { + errNo = NT3HERROR_INVALID_USER_MEMORY_PAGE; + ret = false; + goto end; + } + + dataSend[0] = reg; // store the register + memcpy(&dataSend[1], data, NFC_PAGE_SIZE); + ret = writeTimeout(dataSend, sizeof(dataSend)); + if (ret == false) { + errNo = NT3HERROR_WRITE_USER_MEMORY_PAGE; + goto end; + } + + end: + return ret; +} + + +bool NT3HReadSram(void){ + bool ret=false; + for (int i = SRAM_START_REG, j=0; i<=SRAM_END_REG; i++,j++) { + ret = readTimeout(i, nfcPageBuffer); + if (ret==false) { + return ret; + } + //memcpy(&userData[offset], pageBuffer, sizeof(pageBuffer)); + } + return ret; +} + + +void NT3HGetNxpSerialNumber(uint8_t *buffer) { + uint8_t manuf[16]; + + if (NT3HReaddManufactoringData(manuf)) { + for(int i=0; i<16; i++) { + buffer[i] = manuf[i]; + } + } +} diff --git a/common/iot_nfc/NT3H.h b/common/iot_nfc/NT3H.h new file mode 100644 index 0000000000000000000000000000000000000000..1d4d8a9b8b90985fa00d18a58286086b87d5a442 --- /dev/null +++ b/common/iot_nfc/NT3H.h @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NT3H_H_ +#define NT3H_H_ + +#include "stdbool.h" +#include + +#define NT3H1X_SLAVE_ADDRESS 0x55 +#define MANUFACTORING_DATA_REG 0x0 +#define USER_START_REG 0x1 +#define USER_END_REG 0x77 +#define CONFIG_REG 0x7A +#define SRAM_START_REG 0xF8 +#define SRAM_END_REG 0xFB // just the first 8 bytes +#define SESSION_REG 0xFE +#define NFC_PAGE_SIZE 16 + +typedef enum { + NT3HERROR_NO_ERROR, + NT3HERROR_READ_HEADER, + NT3HERROR_WRITE_HEADER, + NT3HERROR_INVALID_USER_MEMORY_PAGE, + NT3HERROR_READ_USER_MEMORY_PAGE, + NT3HERROR_WRITE_USER_MEMORY_PAGE, + NT3HERROR_ERASE_USER_MEMORY_PAGE, + NT3HERROR_READ_NDEF_TEXT, + NT3HERROR_WRITE_NDEF_TEXT, + NT3HERROR_TYPE_NOT_SUPPORTED +}NT3HerrNo; + +extern uint8_t nfcPageBuffer[NFC_PAGE_SIZE]; +extern NT3HerrNo errNo; + +typedef enum { + NDEFFirstPos, + NDEFMiddlePos, + NDEFLastPos +} RecordPosEnu; +/* + * This strucure is used in the ADD record functionality + * to store the last nfc page information, in order to continue from that point. + */ +typedef struct { + uint8_t page; + uint16_t usedBytes; +} UncompletePageStr; + + +typedef struct { + RecordPosEnu ndefPosition; + uint8_t rtdType; + uint8_t *rtdPayload; + uint16_t rtdPayloadlength; + void *specificRtdData; +}NDEFDataStr; + + +void NT3HGetNxpSerialNumber(uint8_t *buffer); + +/* + * read the user data from the requested page + * first page is 0 + * + * the NT3H1201 has 119 PAges + * the NT3H1101 has 56 PAges (but the 56th page has only 8 Bytes) +*/ +bool NT3HReadUserData(uint8_t page); + +/* + * Write data information from the starting requested page. + * If the dataLen is bigger of NFC_PAGE_SIZE, the consecuiteve needed + * pages will be automatically used. + * + * The functions stops to the latest available page. + * + first page is 0 + * the NT3H1201 has 119 PAges + * the NT3H1101 has 56 PAges (but the 56th page has only 8 Bytes) +*/ +bool NT3HWriteUserData(uint8_t page, const uint8_t* data); + +/* + * The function read the first page of user data where is stored the NFC Header. + * It is important because it contains the total size of all the stored records. + * + * param endRecordsPtr return the value of the total size excluding the NDEF_END_BYTE + * param ndefHeader Store the NDEF Header of the first record + */ +bool NT3HReadHeaderNfc(uint8_t *endRecordsPtr, uint8_t *ndefHeader); + +/* + * The function write the first page of user data where is stored the NFC Header. + * update the bytes that contains the payload Length and the first NDEF Header + * + * param endRecordsPtr The value of the total size excluding the NDEF_END_BYTE + * param ndefHeader The NDEF Header of the first record + */ +bool NT3HWriteHeaderNfc(uint8_t endRecordsPtr, uint8_t ndefHeader); + +bool getSessionReg(void); +bool getNxpUserData(char* buffer); +bool NT3HReadSram(void); +bool NT3HReadSession(void); +bool NT3HReadConfiguration(uint8_t *configuration); + +bool NT3HEraseAllTag(void); + +bool NT3HReaddManufactoringData(uint8_t *manuf) ; + +bool NT3HResetUserData(void); +void NT3H1101_Read_Userpages(uint8_t pagenumber,uint8_t *outbuf); + +#endif /* NFC_H_ */ diff --git a/common/iot_nfc/ndef/ndef.c b/common/iot_nfc/ndef/ndef.c new file mode 100644 index 0000000000000000000000000000000000000000..1b81d2a89693f659024803cf0bd47b8d9ff5320a --- /dev/null +++ b/common/iot_nfc/ndef/ndef.c @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ndef.h" +#include +#include "nfcForum.h" +#include "rtdTypes.h" +#include "NT3H.h" + +typedef uint8_t (*composeRtdPtr)(const NDEFDataStr *ndef, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg); +static composeRtdPtr composeRtd[] = {composeRtdText,composeRtdUri}; + +int16_t firstRecord(UncompletePageStr *page, const NDEFDataStr *data, RecordPosEnu rtdPosition) { + + NDEFRecordStr record; + NDEFHeaderStr header; + uint8_t typeFunct=0; + + switch (data->rtdType){ + case RTD_TEXT: + typeFunct =0; + break; + + case RTD_URI: + typeFunct = 1; + break; + + default: + return -1; + break; + } + + // clear all buffers + memset(&record,0,sizeof(NDEFRecordStr)); + memset(nfcPageBuffer, 0, NFC_PAGE_SIZE); + + // this is the first record + nfcPageBuffer[0] = NDEF_START_BYTE; + composeNDEFMBME(true, true, &record); + uint8_t recordLength; + // prepare the NDEF Header and payload + if(data->rtdPayloadlength>248){ + recordLength = composeRtd[typeFunct](data, &record, &nfcPageBuffer[4]); + header.payloadLength = data->rtdPayloadlength + recordLength; + } + else{ + recordLength = composeRtd[typeFunct](data, &record, &nfcPageBuffer[2]); + header.payloadLength = data->rtdPayloadlength + recordLength; + + } + if(header.payloadLength>255){ + nfcPageBuffer[1] = 0xff; + nfcPageBuffer[2] = header.payloadLength/256; + nfcPageBuffer[3] = header.payloadLength%256; + return 4+recordLength; + } + nfcPageBuffer[1] = header.payloadLength; + return 2+recordLength; + +} + + +int16_t addRecord(UncompletePageStr *pageToUse, const NDEFDataStr *data, RecordPosEnu rtdPosition) { + NDEFRecordStr record; + NDEFHeaderStr header={0}; + uint8_t newRecordPtr, mbMe; + bool ret = true; + uint8_t tmpBuffer[NFC_PAGE_SIZE]; + + uint8_t typeFunct=0; + + switch (data->rtdType){ + case RTD_TEXT: + typeFunct =0; + break; + + case RTD_URI: + typeFunct = 1; + break; + + default: + return -1; + break; + } + + // first Change the Header of the first Record + NT3HReadHeaderNfc(&newRecordPtr, &mbMe); + record.header = mbMe; + composeNDEFMBME(true, false, &record); // this is the first record + mbMe = record.header; + + memset(&record,0,sizeof(NDEFRecordStr)); + memset(tmpBuffer,0,NFC_PAGE_SIZE); + + // prepare second record + uint8_t recordLength = composeRtd[typeFunct](data, &record, tmpBuffer); + + if (rtdPosition == NDEFMiddlePos) { + // this is a record in the middle adjust it on the buffet + composeNDEFMBME(false, false, &record); + } else if (rtdPosition == NDEFLastPos){ + // this is the last record adjust it on the buffet + composeNDEFMBME(false, true, &record); + } + + tmpBuffer[0] = record.header; + + header.payloadLength += data->rtdPayloadlength + recordLength; + + // save the new value of length on the first page + NT3HWriteHeaderNfc((newRecordPtr+header.payloadLength), mbMe); + + + // use the last valid page and start to add the new record + NT3HReadUserData(pageToUse->page); + if (pageToUse->usedBytes+recordLength< NFC_PAGE_SIZE) { + memcpy(&nfcPageBuffer[pageToUse->usedBytes], tmpBuffer, recordLength); + return recordLength+pageToUse->usedBytes; + } else { + uint8_t byteToCopy = NFC_PAGE_SIZE-pageToUse->usedBytes; + memcpy(&nfcPageBuffer[pageToUse->usedBytes], tmpBuffer, byteToCopy); + NT3HWriteUserData(pageToUse->page, nfcPageBuffer); + // update the info with the new page + pageToUse->page++; + pageToUse->usedBytes=recordLength-byteToCopy; + //copy the remain part in the pageBuffer because this is what the caller expect + memcpy(nfcPageBuffer, &tmpBuffer[byteToCopy], pageToUse->usedBytes); + return pageToUse->usedBytes; + } + +} + + + +static bool writeUserPayload(int16_t payloadPtr, const NDEFDataStr *data, UncompletePageStr *addPage){ + uint16_t addedPayload; + bool ret=false; + + uint8_t finish=payloadPtr+data->rtdPayloadlength; + bool endRecord = false; + uint8_t copyByte=0; + + // if the header is less then the NFC_PAGE_SIZE, fill it with the payload + if (NFC_PAGE_SIZE>payloadPtr) { + if (data->rtdPayloadlength > NFC_PAGE_SIZE-payloadPtr) + copyByte = NFC_PAGE_SIZE-payloadPtr; + else + copyByte = data->rtdPayloadlength; + } + + // Copy the payload + memcpy(&nfcPageBuffer[payloadPtr], data->rtdPayload, copyByte); + addedPayload = copyByte; + + + //if it is sufficient one send add the NDEF_END_BYTE + if ((addedPayload >= data->rtdPayloadlength)&&((payloadPtr+copyByte) < NFC_PAGE_SIZE)) { + nfcPageBuffer[(payloadPtr+copyByte)] = NDEF_END_BYTE; + endRecord = true; + } + + ret = NT3HWriteUserData(addPage->page, nfcPageBuffer); + while (!endRecord) { + addPage->page++; // move to a new register + memset(nfcPageBuffer,0,NFC_PAGE_SIZE); + + //special case just the NDEF_END_BYTE remain out + if (addedPayload == data->rtdPayloadlength) { + nfcPageBuffer[0] = NDEF_END_BYTE; + ret = NT3HWriteUserData(addPage->page, nfcPageBuffer); + + endRecord = true; + if (ret == false) { + errNo = NT3HERROR_WRITE_NDEF_TEXT; + } + goto end; + } + + if (addedPayload < data->rtdPayloadlength) { + + // add the NDEF_END_BYTE if there is enough space + if ((data->rtdPayloadlength-addedPayload) < NFC_PAGE_SIZE){ + memcpy(nfcPageBuffer, &data->rtdPayload[addedPayload], (data->rtdPayloadlength-addedPayload)); + nfcPageBuffer[(data->rtdPayloadlength-addedPayload)] = NDEF_END_BYTE; + } else { + memcpy(nfcPageBuffer, &data->rtdPayload[addedPayload], NFC_PAGE_SIZE); + } + + addedPayload += NFC_PAGE_SIZE; + ret = NT3HWriteUserData(addPage->page, nfcPageBuffer); + + if (ret == false) { + errNo = NT3HERROR_WRITE_NDEF_TEXT; + goto end; + } + } else { + endRecord = true; + } + } + + end: + return ret; +} + + +typedef int16_t (*addFunct_T) (UncompletePageStr *page, const NDEFDataStr *data, RecordPosEnu rtdPosition); +static addFunct_T addFunct[] = {firstRecord, addRecord, addRecord}; + +bool NT3HwriteRecord(const NDEFDataStr *data) { + + + uint8_t recordLength=0, mbMe; + UncompletePageStr addPage; + addPage.page = 0; + + + // calculate the last used page + if (data->ndefPosition != NDEFFirstPos ) { + NT3HReadHeaderNfc(&recordLength, &mbMe); + addPage.page = (recordLength+sizeof(NDEFHeaderStr)+1)/NFC_PAGE_SIZE; + + //remove the NDEF_END_BYTE byte because it will overwrite by the new Record + addPage.usedBytes = (recordLength+sizeof(NDEFHeaderStr)+1)%NFC_PAGE_SIZE - 1; + } + + + // call the appropriate function and consider the pointer + // within the NFC_PAGE_SIZE that need to be used + int16_t payloadPtr = addFunct[data->ndefPosition](&addPage, data, data->ndefPosition); + if (payloadPtr == -1) { + errNo = NT3HERROR_TYPE_NOT_SUPPORTED; + return false; + } + return writeUserPayload(payloadPtr, data, &addPage); +} + diff --git a/common/iot_nfc/ndef/ndef.h b/common/iot_nfc/ndef/ndef.h new file mode 100644 index 0000000000000000000000000000000000000000..ea05a1a1c0948ac53244d8ba4beb46afe8c0747f --- /dev/null +++ b/common/iot_nfc/ndef/ndef.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NDEF_H_ +#define NDEF_H_ + +#include "NT3H.h" + +bool NT3HwriteRecord(const NDEFDataStr *data); + +#endif /* NDEF_H_ */ \ No newline at end of file diff --git a/common/iot_nfc/ndef/rtd/nfcForum.c b/common/iot_nfc/ndef/rtd/nfcForum.c new file mode 100644 index 0000000000000000000000000000000000000000..f82f610d66409e3058b8fe524819c024e5dfbdf7 --- /dev/null +++ b/common/iot_nfc/ndef/rtd/nfcForum.c @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nfcForum.h" +#include + +static void rtdHeader(uint8_t type, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg) { + ndefRecord->header |= 1; + ndefRecord->header |= BIT_SR; + I2CMsg[0] = ndefRecord->header; + + ndefRecord->typeLength = 1; + I2CMsg[1] = ndefRecord->typeLength; + + + ndefRecord->type.typeCode=type; + I2CMsg[3] = ndefRecord->type.typeCode; +} + + +uint8_t composeRtdText(const NDEFDataStr *ndef, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg) { + uint8_t retLen; + + uint16_t payLoadLen = addRtdText(&ndefRecord->type.typePayload.text); + + ndefRecord->payloadLength = ndef->rtdPayloadlength+payLoadLen; // added the typePayload + if(ndefRecord->payloadLength>255){ + I2CMsg[0] = 0xc1; + I2CMsg[1] = 0x01; + I2CMsg[2] = 0x00; + I2CMsg[3] = 0x00; + I2CMsg[4]=ndefRecord->payloadLength/256; + I2CMsg[5]=ndefRecord->payloadLength%256; + ndefRecord->type.typeCode=RTD_TEXT; + I2CMsg[6] = ndefRecord->type.typeCode; + memcpy(&I2CMsg[7], &ndefRecord->type.typePayload.text, payLoadLen); + retLen = 6+ /*sizeof(ndefRecord->header) + + sizeof(ndefRecord->typeLength) + + sizeof(ndefRecord->payloadLength) +*/ + 3 + //sizeof(RTDTextTypeStr)-sizeof(TextExtraDataStr) + 1 /*sizeof(ndefRecord->type.typeCode)*/; + + return retLen; + } + ndefRecord->header |= 1; + ndefRecord->header |= BIT_SR; + I2CMsg[0] = ndefRecord->header; + + ndefRecord->typeLength = 1; + I2CMsg[1] = ndefRecord->typeLength; + + I2CMsg[2]=ndefRecord->payloadLength; + + ndefRecord->type.typeCode=RTD_TEXT; + I2CMsg[3] = ndefRecord->type.typeCode; + memcpy(&I2CMsg[4], &ndefRecord->type.typePayload.text, payLoadLen); + retLen = 3 + /*sizeof(ndefRecord->header) + + sizeof(ndefRecord->typeLength) + + sizeof(ndefRecord->payloadLength) +*/ + 3 + //sizeof(RTDTextTypeStr)-sizeof(TextExtraDataStr) + 1 /*sizeof(ndefRecord->type.typeCode)*/; + + return retLen; +} + + +uint8_t composeRtdUri(const NDEFDataStr *ndef, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg) { + + rtdHeader(RTD_URI, ndefRecord, I2CMsg); + + uint8_t payLoadLen = addRtdUriRecord(ndef, &ndefRecord->type.typePayload.uri); + memcpy(&I2CMsg[4], &ndefRecord->type.typePayload.uri, payLoadLen); + + ndefRecord->payloadLength = ndef->rtdPayloadlength+payLoadLen; // added the typePayload + I2CMsg[2]=ndefRecord->payloadLength; + + return 5; + /* retLen = sizeof(ndefRecord->header) + + sizeof(ndefRecord->typeLength) + + sizeof(ndefRecord->payloadLength) + + sizeof(1) + //ndefRecord->type.typePayload.uri.type + sizeof(ndefRecord->type.typeCode); + */ + +} + +void composeNDEFMBME(bool isFirstRecord, bool isLastRecord, NDEFRecordStr *ndefRecord) { + if (isFirstRecord) + ndefRecord->header |= BIT_MB; + else + ndefRecord->header &= ~MASK_MB; + + if (isLastRecord) + ndefRecord->header |= BIT_ME; + else + ndefRecord->header &= ~MASK_ME; +} diff --git a/common/iot_nfc/ndef/rtd/nfcForum.h b/common/iot_nfc/ndef/rtd/nfcForum.h new file mode 100644 index 0000000000000000000000000000000000000000..b58abcad1d6d2030b3fa6b4f78bea2c13c2c3aa6 --- /dev/null +++ b/common/iot_nfc/ndef/rtd/nfcForum.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NFCFORUM_H_ +#define NFCFORUM_H_ + +#include + +#include "rtdTypes.h" +#include "NT3H.h" + +#define NDEF_START_BYTE 0x03 +#define NDEF_END_BYTE 0xFE + +#define NTAG_ERASED 0xD0 + +typedef struct { + uint8_t startByte; + uint16_t payloadLength; +}NDEFHeaderStr; + +#define BIT_MB (1<<7) +#define BIT_ME (1<<6) +#define BIT_CF (1<<5) +#define BIT_SR (1<<4) +#define BIT_IL (1<<3) +#define BIT_TNF (1<<0) +#define MASK_MB 0x80 +#define MASK_ME 0x40 +#define MASK_CF 0x20 +#define MASK_SR 0x10 +#define MASK_IL 0x08 +#define MASK_TNF 0x07 + + + +typedef struct { + uint8_t header; + uint8_t typeLength; + uint16_t payloadLength; + RTDTypeStr type; +}NDEFRecordStr; + +uint8_t composeRtdText(const NDEFDataStr *ndef, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg); +uint8_t composeRtdUri(const NDEFDataStr *ndef, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg); + +void composeNDEFMBME(bool isFirstRecord, bool isLastRecord, NDEFRecordStr *ndefRecord); + +#endif /* NFCFORUM.H_H_ */ diff --git a/common/iot_nfc/ndef/rtd/rtdText.c b/common/iot_nfc/ndef/rtd/rtdText.c new file mode 100644 index 0000000000000000000000000000000000000000..7f9cc58fe0a2f91005a4fd27f6862701b5f6feac --- /dev/null +++ b/common/iot_nfc/ndef/rtd/rtdText.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "rtdText.h" +#include "rtdTypes.h" +#include + +uint8_t addRtdText(RtdTextTypeStr *typeStr) { + + // return addNDEFTextPayload(bodyLength, ndefRecord); + typeStr->status=0x2; + typeStr->language[0]='e'; + typeStr->language[1]='n'; + + return 3; +} + +void prepareText(NDEFDataStr *data, RecordPosEnu position, uint8_t *text) { + data->ndefPosition = position; + data->rtdType = RTD_TEXT; + data->rtdPayload = text; + data->rtdPayloadlength = strlen((const char *)text); + +} diff --git a/common/iot_nfc/ndef/rtd/rtdText.h b/common/iot_nfc/ndef/rtd/rtdText.h new file mode 100644 index 0000000000000000000000000000000000000000..05c662a87c4189710530babe4cd404877d80cb61 --- /dev/null +++ b/common/iot_nfc/ndef/rtd/rtdText.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RTDTEXT_H_ +#define RTDTEXT_H_ + +#include "NT3H.h" + +#define BIT_STATUS (1<<7) +#define BIT_RFU (1<<6) + +#define MASK_STATUS 0x80 +#define MASK_RFU 0x40 +#define MASK_IANA 0b00111111 + +typedef struct { + char *body; + uint8_t bodyLength; +}RtdTextUserPayload; + +typedef struct { + uint8_t status; + uint8_t language[2]; + RtdTextUserPayload rtdPayload; +}RtdTextTypeStr; + + +uint8_t addRtdText(RtdTextTypeStr *typeStr); + +void prepareText(NDEFDataStr *data, RecordPosEnu position, uint8_t *text); +#endif /* NDEFTEXT_H_ */ diff --git a/common/iot_nfc/ndef/rtd/rtdTypes.h b/common/iot_nfc/ndef/rtd/rtdTypes.h new file mode 100644 index 0000000000000000000000000000000000000000..e1169d5941295648b72105970ea5f9aa34d61a40 --- /dev/null +++ b/common/iot_nfc/ndef/rtd/rtdTypes.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RTDTYPES_H_ +#define RTDTYPES_H_ + +#include "rtdText.h" +#include "rtdUri.h" + + +#define RTD_TEXT 'T' +#define RTD_URI 'U' + +typedef union { + RtdTextTypeStr text; + RTDUriTypeStr uri; +} RTDTypeUnion; + +typedef struct { + uint8_t typeCode; + RTDTypeUnion typePayload; +}RTDTypeStr; + +#endif /* RTDTYPES_H_ */ diff --git a/common/iot_nfc/ndef/rtd/rtdUri.c b/common/iot_nfc/ndef/rtd/rtdUri.c new file mode 100644 index 0000000000000000000000000000000000000000..a280103904c2a359b8b5f21333a17cf1a3e80178 --- /dev/null +++ b/common/iot_nfc/ndef/rtd/rtdUri.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "rtdUri.h" +#include +#include "rtdTypes.h" + + + RTDUriTypeStr uri; + +uint8_t addRtdUriRecord(const NDEFDataStr *ndef, RTDUriTypeStr *uriType) { + + + uriType->type=((RTDUriTypeStr*) ndef->specificRtdData)->type; + + return 1; +} + +void prepareUrihttp(NDEFDataStr *data, RecordPosEnu position, uint8_t *text) { + data->ndefPosition = position; + data->rtdType = RTD_URI; + data->rtdPayload = text; + data->rtdPayloadlength = strlen((const char *)text);; + + uri.type = httpWWW; + data->specificRtdData = &uri; +} diff --git a/common/iot_nfc/ndef/rtd/rtdUri.h b/common/iot_nfc/ndef/rtd/rtdUri.h new file mode 100644 index 0000000000000000000000000000000000000000..260485cd8292c328156e8001b994281fcdd24334 --- /dev/null +++ b/common/iot_nfc/ndef/rtd/rtdUri.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "NT3H.h" + +#ifndef RTDURI_H_ +#define RTDURI_H_ + +typedef enum { + freeForm, //0x00 No prepending is done ... the entire URI is contained in the URI Field + httpWWW, //0x01 http://www. + httpsWWW, //0x02 https://www. + http, //0x03 http:// + https, //0x04 https:// + tel, //0x05 tel: + mailto, //0x06 mailto: + ftpAnonymous,//0x07 ftp://anonymous:anonymous@ + ftpFtp, //0x08 ftp://ftp. + ftps, //0x09 ftps:// + sftp, //0x0A sftp:// + smb, //0x0B smb:// + nfs, //0x0C nfs:// + ftp, //0x0D ftp:// + dav, //0x0E dav:// + news, //0x0F news: + telnet, //0x10 telnet:// + imap, //0x11 imap: + rtps, //0x12 rtsp:// + urn, //0x13 urn: + /* + 0x14 pop: + 0x15 sip: + 0x16 sips: + 0x17 tftp: + 0x18 btspp:// + 0x19 btl2cap:// + 0x1A btgoep:// + 0x1B tcpobex:// + 0x1C irdaobex:// + 0x1D file:// + 0x1E urn:epc:id: + 0x1F urn:epc:tag: + 0x20 urn:epc:pat: + 0x21 urn:epc:raw: + 0x22 urn:epc: + 0x23 urn:nfc: + */ +}UriTypeE; + +typedef struct { + char *body; + uint8_t bodyLength; + void *extraData; // herre should be stored specific URI msgs +}RtdUriUserPayload; + +typedef struct { + UriTypeE type; + RtdUriUserPayload userPayload; // here should be stored specific URI msgs +}RTDUriTypeStr; + +uint8_t addRtdUriRecord(const NDEFDataStr *ndef, RTDUriTypeStr *typeStr); + +void prepareUrihttp(NDEFDataStr *data, RecordPosEnu position, uint8_t *text); + +#endif /* RTDURI_H_ */ diff --git a/common/iot_nfc/nfc.c b/common/iot_nfc/nfc.c new file mode 100644 index 0000000000000000000000000000000000000000..65a432e728ee93a9470181e71dfe7cf41b6a9d0a --- /dev/null +++ b/common/iot_nfc/nfc.c @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "rtdText.h" +#include "rtdUri.h" +#include "ndef.h" +#include "nfc.h" + +bool storeUrihttp(RecordPosEnu position, uint8_t *http){ + + NDEFDataStr data; + + + prepareUrihttp(&data, position, http); + return NT3HwriteRecord( &data ); +} + +bool storeText(RecordPosEnu position, uint8_t *text){ + NDEFDataStr data; + prepareText(&data, position, text); + return NT3HwriteRecord( &data ); +} diff --git a/common/iot_nfc/nfc.h b/common/iot_nfc/nfc.h new file mode 100644 index 0000000000000000000000000000000000000000..90370206b35dc4e0919d110bbccfec778723eff4 --- /dev/null +++ b/common/iot_nfc/nfc.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _NFC_H_ +#define _NFC_H_ + +#include "NT3H.h" + +/* + * The function write in the NT3H a new URI Rtd on the required position + * + * param: + * position: where add the record + * http: the address to write + * + */ +bool storeUrihttp(RecordPosEnu position, uint8_t *http); + + +/* + * The function write in the NT3H a new Text Rtd on the required position + * + * param: + * position: where add the record + * text: the text to write + * + */ +bool storeText(RecordPosEnu position, uint8_t *text); + +#endif /* NFC_H_ */ diff --git a/common/iot_nfc/nfc_app.c b/common/iot_nfc/nfc_app.c new file mode 100644 index 0000000000000000000000000000000000000000..848f548116674d17f1b2f21084f8974279e00cf7 --- /dev/null +++ b/common/iot_nfc/nfc_app.c @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "nfc_app.h" +#include "NT3H.h" +#include "iot_nfc.h" +#include +#include +#include +#include + +#define HI_IO_FUNC_GPIO_1_I2C1_SCL_6 6 +#define I2C_IDX_BAUDRATE (400000) + +#define CONST_2 2 +#define CONST_3 3 +#define CONST_4 4 +#define CONST_8 8 +#define CONST_9 9 +#define CONST_10 10 +#define CONST_15 15 +#define CONST_16 16 +#define CONST_32 32 +#define CONST_35 35 +#define CONST_48 48 +#define CONST_64 64 +#define CONST_65 65 +#define CONST_96 96 +#define CONST_97 97 +#define CONST_126 126 +#define CONST_512 512 + +char product[50], wifissid[50], wifipwd[50], deviceid[50], devicepwd[50], nodeid[20]; + +/** + * @brief NFC JSON数据中,获取WiFi的ssid和密钥及设备DevicesID和密钥 + * + * @return uint8_t + */ +uint8_t nfc_get_devived_data(void) { + uint8_t memary_buf[CONST_16 * CONST_15]; + cJSON *json, *jsonTemp; // *jsonArray, + uint8_t jsonbuf[CONST_512 * CONST_2] = {0}; + char jsonbuf_string[CONST_512 * CONST_2] = {0}; + uint8_t payload_len = 0; + uint8_t json_len = 0; + + NT3H1101_Read_Userpages(CONST_15, memary_buf); + + payload_len = memary_buf[CONST_4]; + json_len = payload_len - CONST_3; + + printf("payload_len: %d", payload_len); + //取出真实的json数据包,往后再偏移3位 + for (uint8_t i = 0; i < json_len; i++) { + jsonbuf[i] = memary_buf[CONST_9 + i]; + } + memset(jsonbuf_string, 0x00, sizeof(jsonbuf_string)); + sprintf(jsonbuf_string, "%s", jsonbuf); + + printf("nfc data: %s", jsonbuf_string); + //解析json数据 + json = cJSON_Parse(jsonbuf_string); + if (!json) { + printf("Error before: [%s]\n", cJSON_GetErrorPtr()); + return -1; + } else { + jsonTemp = cJSON_GetObjectItem(json, "product"); + memset(product, 0, sizeof(product)); + snprintf(product, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring); + jsonTemp = cJSON_GetObjectItem(json, "device_id"); + memset(deviceid, 0, sizeof(deviceid)); + snprintf(deviceid, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring); + jsonTemp = cJSON_GetObjectItem(json, "secret"); + memset(devicepwd, 0, sizeof(devicepwd)); + snprintf(devicepwd, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring); + jsonTemp = cJSON_GetObjectItem(json, "ssid"); + memset(wifissid, 0, sizeof(wifissid)); + snprintf(wifissid, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring); + jsonTemp = cJSON_GetObjectItem(json, "pwd"); + memset(wifipwd, 0, sizeof(wifipwd)); + snprintf(wifipwd, strlen(jsonTemp->valuestring) + 1, "%s", jsonTemp->valuestring); + + cJSON_Delete(json); + free(json); // isequal + } + return 0; +} + +char byte2char(uint8_t num) { + char ch; + if (num >= CONST_32 && num <= CONST_64) { + ch = '0' + (num - CONST_48); + } else if (num >= CONST_65 && num <= CONST_96) { + ch = 'A' + (num - CONST_65); + } else if (num >= CONST_97 && num <= CONST_126) { + ch = 'a' + (num - CONST_97); + } else { + ch = '0'; + } + return ch; +} + +/** + * @brief Get the nfc device data object + * + * @return uint8_t + */ +uint8_t get_nfc_device_data(void) { + uint8_t memary_buf[16 * 15] = {0}; + uint8_t payload_len = 0; + uint8_t start_index = 0; // 取值开始位置 + uint8_t count = 0; // 取值数量 + NT3H1101_Read_Userpages(CONST_15, memary_buf); + // 符号SOH+NUL 和 表示1的正文开始,如果不符合,不进行读取 + if (!(memary_buf[CONST_8] == 1 && memary_buf[CONST_9] == 0 && byte2char(memary_buf[CONST_35 + 1]) == '1')) { + return -1; + } + payload_len = memary_buf[CONST_35]; // 十进制的有效数据总长度 + start_index = CONST_35 + CONST_4; // 表示长度的位置+4为数据位置 + count = (memary_buf[CONST_35 + CONST_2] - CONST_48) * CONST_10 + (memary_buf[CONST_35 + CONST_3] - CONST_48); + + // 03 9B D2 02 96 68 77 20 01 00 48 00 39 41 34 39 00 81 07 00 07 20 06 85 14 91 14 06 00 00 00 00 00 00 91 6B 31 32 34 36 33 31 35 63 39 33 63 36 36 34 63 36 66 37 39 33 38 64 63 32 30 38 37 32 30 38 4C 61 6D 70 5F 30 30 32 33 32 36 31 32 33 34 35 36 41 42 43 44 45 46 47 31 32 33 34 35 36 41 42 43 44 45 46 47 34 30 31 31 35 31 30 74 65 61 6D 58 2D 4C 61 6D 70 36 32 30 56 56 56 56 56 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 8B 0C 12 51 C2 6D 52 32 84 34 A1 82 65 9C FE + // 124 6315c93c664c6f7938dc2087 208 Lamp_002 326 123456ABCDEFG123456ABCDEFG 401 1 510 teamX-Lamp 620 VVVVVXXXXXXXXXXXXXXX + // product_id+_+node_id,pwd + + for (int i = 0; i < count; i++) { + product[i] = byte2char(memary_buf[start_index + i]); + } + printf("product_id=%s\n", product); + // 下一个起始位置和长度 + start_index += count + CONST_3; + count = (memary_buf[start_index - CONST_2] - CONST_48) * CONST_10 + (memary_buf[start_index - 1] - CONST_48); + for (int i = 0; i < count; i++) { + nodeid[i] = byte2char(memary_buf[start_index + i]); + } + printf("node_id=%s\n", nodeid); + // 下一个起始位置和长度 + start_index += count + CONST_3; + count = (memary_buf[start_index - CONST_2] - CONST_48) * CONST_10 + (memary_buf[start_index - 1] - CONST_48); + for (int i = 0; i < count; i++) { + devicepwd[i] = byte2char(memary_buf[start_index + i]); + } + + sprintf(deviceid, "%s_%s", product, nodeid); + + printf("device_id=%s,secret=%s\n", deviceid, devicepwd); + + return 0; +} +int BOARD_InitNfc(void) { + // GPIO_1复用为I2C1_SCL + IoTGpioSetFunc(1, HI_IO_FUNC_GPIO_1_I2C1_SCL_6); + // baudrate: 400kbps + IoTI2cInit(1, I2C_IDX_BAUDRATE); + return 0; +} + +int BOARD_GetNfcInfo(NfcInfo *info) { + int ret = -1; + if (info == NULL) { + return ret; + } + + if (nfc_get_devived_data() == 0) { + info->deviceID = (const char *)deviceid; + info->devicePWD = (const char *)devicepwd; + info->wifiSSID = (const char *)wifissid; + info->wifiPWD = (const char *)wifipwd; + ret = 0; + } else if (get_nfc_device_data() == 0) { + info->deviceID = (const char *)deviceid; + info->devicePWD = (const char *)devicepwd; + ret = 0; + } + return ret; +} diff --git a/common/iot_nfc/nfc_app.h b/common/iot_nfc/nfc_app.h new file mode 100644 index 0000000000000000000000000000000000000000..78088819598f7e322c68b1c938133eee7d93644b --- /dev/null +++ b/common/iot_nfc/nfc_app.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __NFC_APP_H__ +#define __NFC_APP_H__ + +#include "nfc.h" + +uint8_t nfc_get_devived_data(void); +/** + * @brief 数字管家NFC数据 + * + * @return uint8_t 0 成功 + */ +uint8_t get_nfc_device_data(void); +/** + * @brief 初始化NFC板卡 + * + * @return int + */ +int BOARD_InitNfc(void); +#endif \ No newline at end of file diff --git a/common/iot_schedule/BUILD.gn b/common/iot_schedule/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a460762ae197655581159fc06f750b5082de2bf5 --- /dev/null +++ b/common/iot_schedule/BUILD.gn @@ -0,0 +1,34 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_schedule") { + sources = [ + "iot_schedule.c", + "schedule_list.c", + "schedule_store.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//foundation/communication/wifi_lite/interfaces/wifiservice", + "../inc", + ] + + deps = [ + + ] + +} \ No newline at end of file diff --git a/common/iot_schedule/iot_schedule.c b/common/iot_schedule/iot_schedule.c new file mode 100644 index 0000000000000000000000000000000000000000..3b5bba16117163d4cef2ffa7e77a93e205aedbf4 --- /dev/null +++ b/common/iot_schedule/iot_schedule.c @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "schedule_list.h" +#include "schedule_store.h" +#include "iot_schedule.h" + +#define MAX_SCHEDULE_LIST 7 + +static SCHEDULE_LIST gScheduleList[MAX_SCHEDULE_LIST]; +static int g_isUpdate = 0; +static int g_startSeconds = -1; +static int g_durationSeconds = -1; +static int g_curWeekDay = -1; +static ScheduleCommand g_cmd = {0}; +static int g_oneTime = 0; + +static int GetWeekDay(unsigned char weekday, int Days[], int size) +{ + int n = 0; + if (size != MAX_SCHEDULE_LIST) { + SCH_ERR("weekday array is OOR! \n"); + return -1; + } + + for (int i = 0; i < MAX_SCHEDULE_LIST; i++) { + if ((weekday >> i) & 0x01) { + Days[n++] = i; + } + } + + return n; +} + +static void ScheduleGetStore(void) +{ + int total = ScheduleStoreGetTotal(); + if (total <= 0) { + SCH_ERR("no schedule stored! \n"); + return; + } + + for (int i = 0; i < total; i++) { + ScheduleInfo info = {0}; + int weeknum = 0; + int week[MAX_SCHEDULE_LIST] = {0}; + + if (ScheduleStoreGetInfo(&info, i) < 0) { + SCH_ERR("read schedulestore %d failed! \n", i); + continue; + } + + weeknum = GetWeekDay(info.week, week, MAX_SCHEDULE_LIST); + if (weeknum <= 0) { + SCH_ERR("the schedule has no weekday info! \n"); + continue; + } + + for (int j = 0; j < weeknum; j++) { + int weekday = week[j]; + if (weekday >= MAX_SCHEDULE_LIST) { + SCH_ERR("weekday is OOR!\n"); + break; + } + if (ScheduleListAppend(gScheduleList[weekday], &info) < 0) { + SCH_ERR("ScheduleListAppend failed!\n"); + break; + } + SCH_DBG("add info : id--%s, starttime--%d, duration--%d, week(%d)\n", + info.id, info.starttime, info.duration, weekday); + } + } +} + +int IOT_ScheduleInit(void) +{ + int retval = 0; + + for (int i = 0; i < MAX_SCHEDULE_LIST; i++) { + gScheduleList[i] = ScheduleListCreate(NULL); + if (gScheduleList[i] == NULL) { + SCH_ERR("ScheduleListCreate (%d) failed! \n", i); + retval = -1; + break; + } + } + + if (retval < 0) { + for (int i = 0; i < MAX_SCHEDULE_LIST; i++) { + if (gScheduleList[i] != NULL) { + ScheduleListDestroy(gScheduleList[i]); + gScheduleList[i] = NULL; + } + } + } else { + ScheduleGetStore(); + g_isUpdate = 1; + } + + return retval; +} + +static int ScheduleUpdate(const char *id, int *day, int size, int startTime, int durTime, + SCHEDULE_OPTION option, int cmd, int value) +{ +typedef int (*OptionFunc)(SCHEDULE_LIST mHandle, ScheduleInfo *info); + OptionFunc opFunc[] = {ScheduleListAppend, ScheduleListUpdate, ScheduleListDelete}; + ScheduleInfo info = {0}; + info.starttime = startTime; + info.duration = durTime; + info.scheduleCmd.cmd = cmd; + info.scheduleCmd.value = value; + + SCH_DBG("cmd = %d, value = %d \n", cmd, value); + + if (option > SCHEDULE_OPTION_DELETE) { + SCH_ERR("OPTION OOR!\n"); + return -1; + } + if (size == 1 && day[0] == 0) { // one time schedule + g_oneTime = 1; + g_isUpdate = 1; + g_startSeconds = startTime; + g_durationSeconds = durTime; + g_cmd.value = value; + g_cmd.cmd = cmd; + SCH_DBG("this schedule is onetime! \n"); + return 0; + } + + strncpy(info.id, id, strlen(id)); + + for (int i = 0; i < size; i++) { + int idx = day[i] - 1; + if (idx >= MAX_SCHEDULE_LIST) { + SCH_ERR("weekday is OOR! \n"); + continue; + } + info.week += (1 << idx); + + if (opFunc[option](gScheduleList[idx], &info) < 0) { + SCH_ERR("ScheduleListAppend failed! \n"); + return -1; + } + SCH_DBG("add info : id--%s, starttime--%d, duration--%d, week(%d)--0x%x, cmd--%d, value -- %d\n", + info.id, info.starttime, info.duration, idx, info.week, info.scheduleCmd.cmd, info.scheduleCmd.value); + } + + ScheduleStoreUpdate(&info, option); + + if ((option == SCHEDULE_OPTION_DELETE) && (g_startSeconds == startTime)) { + g_startSeconds = 0; + g_durationSeconds = 0; + } + + g_isUpdate = 1; + return 0; +} + +int IOT_ScheduleAdd(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value) +{ + return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_ADD, cmd, value); +} + +int IOT_ScheduleUpdate(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value) +{ + return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_UPDATE, cmd, value); +} + +int IOT_ScheduleDelete(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value) +{ + return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_DELETE, cmd, value); +} + +int IOT_ScheduleIsUpdate(unsigned int weekday, int curtime) +{ + int total = 0; + int retval = 0; + + if (g_isUpdate == 0) { + return 0; + } + + g_isUpdate = 0; + + if (g_oneTime) { + g_oneTime = 0; + if (g_startSeconds > curtime) { + SCH_DBG("get one time schedule! \n"); + return 1; + } + } + + + if (weekday >= MAX_SCHEDULE_LIST || curtime < 0) { + return 0; + } + + total = ScheduleListGetSize(gScheduleList[weekday]); + if (total <= 0) { + return 0; + } + + for (int i = 0; i < total; i++) { + ScheduleInfo info; + if (ScheduleListGet(gScheduleList[weekday], i, (ScheduleInfo *)&info) < 0) { + SCH_ERR("ScheduleListGet failed! \n"); + return 0; + } + + if (curtime < (info.starttime + info.duration)) { + if (g_curWeekDay == weekday) { + if (g_startSeconds != info.starttime || + g_durationSeconds != info.duration) { + g_startSeconds = info.starttime; + g_durationSeconds = info.duration; + g_cmd.cmd = info.scheduleCmd.cmd; + g_cmd.value = info.scheduleCmd.value; + retval = 1; + } + } else { + g_curWeekDay = weekday; + g_startSeconds = info.starttime; + g_durationSeconds = info.duration; + g_cmd.cmd = info.scheduleCmd.cmd; + g_cmd.value = info.scheduleCmd.value; + retval = 1; + } + break; + } + } + + if (retval == 1) { + SCH_DBG("cmd = %d, value = %d \n", g_cmd.cmd, g_cmd.value); + } + + return retval; +} + +void IOT_ScheduleSetUpdate(int update) +{ + g_isUpdate = update; +} + +int IOT_ScheduleGetStartTime(void) +{ + return g_startSeconds; +} + +int IOT_ScheduleGetDurationTime(void) +{ + return g_durationSeconds; +} + +int IOT_ScheduleGetCommand(int *cmd, int *value) +{ + if (cmd != NULL) { + *cmd = g_cmd.cmd; + } + + if (value != NULL) { + *value = g_cmd.value; + } + + return 0; +} + +void IOT_ScheduleDeinit(void) +{ + for (int i = 0; i < MAX_SCHEDULE_LIST; i++) { + if (gScheduleList[i] != NULL) { + ScheduleListDestroy(gScheduleList[i]); + gScheduleList[i] = NULL; + } + } +} diff --git a/common/iot_schedule/schedule_list.c b/common/iot_schedule/schedule_list.c new file mode 100644 index 0000000000000000000000000000000000000000..15207a0f44b071cd1c2ac16c4608369cf402122e --- /dev/null +++ b/common/iot_schedule/schedule_list.c @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "schedule_list.h" + +typedef struct __ScheduleNode { + ScheduleInfo *data; + struct __ScheduleNode *prev; + struct __ScheduleNode *next; +} ScheduleNode; + +typedef struct { + ScheduleNode *head, *tail; + int total; +} ScheduleList; + +static ScheduleNode *ScheduleNodeCreate(ScheduleInfo *info) +{ + ScheduleNode *pNode; + + if (info == NULL) { + SCH_ERR("NULL POINT! \n"); + return NULL; + } + + pNode = (ScheduleNode *)malloc(sizeof(ScheduleNode)); + if (pNode == NULL) { + SCH_ERR("OOM!\n"); + return NULL; + } + + pNode->data = (char *)malloc(sizeof(ScheduleInfo)); + if (pNode->data == NULL) { + SCH_ERR("OOM!\n"); + free(pNode); + return NULL; + } + + memcpy(pNode->data, info, sizeof(ScheduleInfo)); + pNode->prev = NULL; + pNode->next = NULL; + + return pNode; +} + +static void ScheduleNodeDestroy(ScheduleNode *pNode) +{ + if (pNode == NULL) { + return; + } + if (pNode->data) { + free(pNode->data); + pNode->data = NULL; + } + free(pNode); + pNode = NULL; +} + +static ScheduleNode *ScheduleGetNodeWithTime(ScheduleNode *head, int time) +{ + ScheduleNode *pNode = head; + while (pNode) { + if (pNode->data && (time <= pNode->data->starttime)) { + break; + } + pNode = pNode->next; + } + + return pNode; +} + +static ScheduleNode *ScheduleGetNodeWithId(ScheduleNode *head, const char *id) +{ + ScheduleNode *pNode = head; + if (id == NULL) { + return NULL; + } + while (pNode) { + if (pNode->data && (strcmp(pNode->data->id, id) == 0)) { + break; + } + pNode = pNode->next; + } + + return pNode; +} + +static ScheduleNode *ScheduleGetNodeWithIdx(ScheduleNode *head, int idx) +{ + ScheduleNode *pNode = head; + for (int i = 0; i < idx; i++) { + if (pNode) { + pNode = pNode->next; + } + } + + return pNode; +} + +SCHEDULE_LIST ScheduleListCreate(ScheduleInfo *info) +{ + ScheduleNode *pNode; + ScheduleList *list = (ScheduleList *)malloc(sizeof(ScheduleList)); + if (list == NULL) { + SCH_ERR("OOM!\n"); + return NULL; + } + + pNode = ScheduleNodeCreate(info); + if (pNode != NULL) { + list->total = 1; + } else { + list->total = 0; + } + list->head = list->tail = pNode; + + return (SCHEDULE_LIST)list; +} + +int ScheduleListAppend(SCHEDULE_LIST mHandle, ScheduleInfo *info) +{ + ScheduleNode *pNode; + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL || info == NULL) { + SCH_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ScheduleNodeCreate(info); + if (pNode == NULL) { + SCH_ERR("ScheduleNodeCreate failed! \n"); + return -1; + } + + if (list->head == NULL) { + list->head = list->tail = pNode; + } else { + ScheduleNode *temp = ScheduleGetNodeWithTime(list->head, info->starttime); + if (temp == NULL) { + pNode->prev = list->tail; + list->tail->next = pNode; + list->tail = pNode; + } else { + pNode->prev = temp->prev; + pNode->next = temp; + if (temp->prev != NULL) { + temp->prev->next = pNode; + } else { + list->head = pNode; + } + temp->prev = pNode; + } + } + + list->total++; + + return 0; +} + +int ScheduleListUpdate(SCHEDULE_LIST mHandle, ScheduleInfo *info) +{ + int retval = 0; + ScheduleNode *pNode; + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL || info == NULL) { + SCH_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ScheduleGetNodeWithId(list->head, info->id); + if (pNode != NULL) { + memcpy(pNode->data, info, sizeof(ScheduleInfo)); + } else { + retval = ScheduleListAppend(mHandle, info); + } + + return retval; +} + +int ScheduleListDelete(SCHEDULE_LIST mHandle, ScheduleInfo *info) +{ + ScheduleNode *pNode, *prev, *next; + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL || info == NULL) { + SCH_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ScheduleGetNodeWithId(list->head, info->id); + if (pNode == NULL) { + return -1; + } + prev = pNode->prev; + next = pNode->next; + + if (prev == NULL && next == NULL) { + list->head = list->tail = NULL; + } else if (prev == NULL && next != NULL) { + next->prev = NULL; + list->head = next; + } else if (prev != NULL && next == NULL) { + prev->next = NULL; + list->tail = prev; + } else { + prev->next = next; + next->prev = prev; + } + pNode->next = NULL; + pNode->prev = NULL; + list->total--; + ScheduleNodeDestroy(pNode); + + return 0; +} + +int ScheduleListGetSize(SCHEDULE_LIST mHandle) +{ + if (mHandle == NULL) { + return -1; + } + + return ((ScheduleList *)mHandle)->total; +} + +int ScheduleListGet(SCHEDULE_LIST mHandle, int idx, ScheduleInfo *info) +{ + ScheduleNode *pNode; + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL || info == NULL) { + SCH_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ScheduleGetNodeWithIdx(list->head, idx); + if (pNode == NULL || pNode->data == NULL) { + SCH_ERR("no such node(%d) \n", idx); + return -1; + } + + memcpy(info, pNode->data, sizeof(ScheduleInfo)); + + return 0; +} + +void ScheduleListDestroy(SCHEDULE_LIST mHandle) +{ + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL) { + return; + } + while (list->head) { + ScheduleNode *pNode = list->head; + list->head = pNode->next; + pNode->next = NULL; + list->head->prev = NULL; + + ScheduleNodeDestroy(pNode); + } + list->total = 0; + + free(list); + list = NULL; +} diff --git a/common/iot_schedule/schedule_store.c b/common/iot_schedule/schedule_store.c new file mode 100644 index 0000000000000000000000000000000000000000..6b02a35b5247e5e266ec1064c1445841c7c5e57e --- /dev/null +++ b/common/iot_schedule/schedule_store.c @@ -0,0 +1,289 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "ohos_errno.h" +#include "ohos_types.h" +#include "utils_config.h" +#include "utils_file.h" + +#include "schedule_store.h" + +#define SCHEDULE_KEY "shcedule_info" +#define TEMP_KEY "info.tmp" + +static int SearchScheduleInfo(const char *id) +{ + int idx = -1; + int total = 0; + int file; + + if (id == NULL) { + return -1; + } + + file = UtilsFileOpen(SCHEDULE_KEY, O_RDWR_FS, 0); + if (file < 0) { + return -1; + } + + UtilsFileSeek(file, 0, SEEK_SET_FS); + if (UtilsFileRead(file, (char *)&total, sizeof(int)) < 0) { + UtilsFileClose(file); + return -1; + } + + for (int i = 0; i < total; i++) { + ScheduleInfo info; + int off = sizeof(ScheduleInfo) * i + sizeof(int); + if (UtilsFileSeek(file, off, SEEK_SET_FS) < 0) { + UtilsFileClose(file); + return -1; + } + if (UtilsFileRead(file, (char *)&info, sizeof(ScheduleInfo)) < 0) { + UtilsFileClose(file); + return -1; + } + if (strncmp((const char *)info.id, id, strlen(id)) == 0) { + idx = i; + break; + } + } + + UtilsFileClose(file); + + return idx; +} + +static int DeletScheduleStore(ScheduleInfo *info) +{ + int fd; + int tmp; + int idx; + int total; + if (info == NULL) { + return -1; + } + + idx = SearchScheduleInfo((const char *)info->id); + if (idx < 0) { + return -1; + } + + fd = UtilsFileOpen(SCHEDULE_KEY, O_RDWR_FS, 0); + if (fd < 0) { + return -1; + } + + UtilsFileSeek(fd, 0, SEEK_SET_FS); + if (UtilsFileRead(fd, (char *)&total, sizeof(int)) < 0) { + UtilsFileClose(fd); + return -1; + } + if (total <= 0) { + UtilsFileClose(fd); + return -1; + } else if (total == 1) { + UtilsFileClose(fd); + UtilsFileDelete(SCHEDULE_KEY); + return 0; + } + + total -= 1; + + tmp = UtilsFileOpen(TEMP_KEY, O_CREAT_FS | O_RDWR_FS, 0); + if (tmp < 0) { + UtilsFileClose(fd); + return -1; + } + + if (UtilsFileWrite(tmp, (char *)&total, sizeof(int)) < 0) { + UtilsFileClose(fd); + UtilsFileClose(tmp); + return -1; + } + + for (int i = 0; i <= total; i++) { + ScheduleInfo tmpInfo; + if (UtilsFileRead(fd, (char *)&tmpInfo, sizeof(ScheduleInfo)) < 0) { + UtilsFileClose(fd); + UtilsFileClose(tmp); + return -1; + } + if (i == idx) { + continue; + } + if (UtilsFileWrite(tmp, (char *)&tmpInfo, sizeof(ScheduleInfo)) < 0) { + UtilsFileClose(fd); + UtilsFileClose(tmp); + return -1; + } + } + + UtilsFileClose(fd); + UtilsFileClose(tmp); + + UtilsFileDelete(SCHEDULE_KEY); + UtilsFileCopy(TEMP_KEY, SCHEDULE_KEY); + UtilsFileDelete(TEMP_KEY); + + return 0; +} + +static int UpdateScheduleStore(ScheduleInfo *info) +{ + int fd, idx; + + if (info == NULL) { + return -1; + } + + idx = SearchScheduleInfo((const char *)info->id); + if (idx < 0) { + return -1; + } + + fd = UtilsFileOpen(SCHEDULE_KEY, O_RDWR_FS, 0); + if (fd < 0) { + return -1; + } + + if (UtilsFileSeek(fd, idx * sizeof(ScheduleInfo) + sizeof(int), SEEK_SET_FS) < 0) { + UtilsFileClose(fd); + return -1; + } + + if (UtilsFileWrite(fd, (char *)info, sizeof(ScheduleInfo)) < 0) { + UtilsFileClose(fd); + return -1; + } + + UtilsFileClose(fd); + + return 0; +} + +static int AddScheduleStore(ScheduleInfo *info) +{ + int total = 0; + int fd; + + if (info == NULL) { + return -1; + } + + fd = UtilsFileOpen(SCHEDULE_KEY, O_RDWR_FS | O_CREAT_FS | O_APPEND_FS, 0); + if (fd < 0) { + return -1; + } + UtilsFileSeek(fd, 0, SEEK_SET_FS); + if (UtilsFileRead(fd, (char *)&total, sizeof(int)) < 0) { + UtilsFileClose(fd); + return -1; + } + total += 1; + UtilsFileSeek(fd, 0, SEEK_SET_FS); + if (UtilsFileWrite(fd, (char *)&total, sizeof(int)) < 0) { + UtilsFileClose(fd); + return -1; + } + + UtilsFileSeek(fd, 0, SEEK_END_FS); + if (UtilsFileWrite(fd, (char *)info, sizeof(ScheduleInfo)) < 0) { + UtilsFileClose(fd); + return -1; + } + + UtilsFileClose(fd); + + return 0; +} + +int ScheduleStoreUpdate(ScheduleInfo *info, SCHEDULE_OPTION option) +{ + int retval = -1; + switch (option) { + case SCHEDULE_OPTION_ADD: + AddScheduleStore(info); + break; + case SCHEDULE_OPTION_UPDATE: + UpdateScheduleStore(info); + break; + case SCHEDULE_OPTION_DELETE: + DeletScheduleStore(info); + break; + default: + break; + } + +} + +int ScheduleStoreGetTotal(void) +{ + int total = 0; + int fd = UtilsFileOpen(SCHEDULE_KEY, O_RDWR_FS, 0); + if (fd < 0) { + return -1; + } + + if (UtilsFileRead(fd, (char *)&total, sizeof(int)) < 0) { + total = -1; + } + UtilsFileClose(fd); + + return total; +} + +int ScheduleStoreGetInfo(ScheduleInfo *info, int idx) +{ + int total = 0; + int fd = UtilsFileOpen(SCHEDULE_KEY, O_RDWR_FS, 0); + if (fd < 0) { + return -1; + } + + if (UtilsFileRead(fd, (char *)&total, sizeof(int)) < 0) { + UtilsFileClose(fd); + return -1; + } + + if (idx >= total) { + UtilsFileClose(fd); + return -1; + } + + if (UtilsFileSeek(fd, idx * sizeof(ScheduleInfo) + sizeof(int), SEEK_SET_FS) < 0) { + UtilsFileClose(fd); + return -1; + } + + if (UtilsFileRead(fd, (char *)info, sizeof(ScheduleInfo)) < 0) { + UtilsFileClose(fd); + return -1; + } + + UtilsFileClose(fd); + + return 0; +} + +void ScheduleStoreDelete(void) +{ + UtilsFileDelete(SCHEDULE_KEY); +} diff --git a/common/iot_schedule_posix/BUILD.gn b/common/iot_schedule_posix/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9f3468b8cc5b5522ac17a49f46bde6be20e1bb3a --- /dev/null +++ b/common/iot_schedule_posix/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_schedule_posix") { + sources = [ + "iot_schedule.c", + "schedule_list.c", + "schedule_store.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/", + "//foundation/communication/wifi_lite/interfaces/wifiservice", + "../inc", + ] + + deps = [ + + ] + +} \ No newline at end of file diff --git a/common/iot_schedule_posix/iot_schedule.c b/common/iot_schedule_posix/iot_schedule.c new file mode 100644 index 0000000000000000000000000000000000000000..3b5bba16117163d4cef2ffa7e77a93e205aedbf4 --- /dev/null +++ b/common/iot_schedule_posix/iot_schedule.c @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "schedule_list.h" +#include "schedule_store.h" +#include "iot_schedule.h" + +#define MAX_SCHEDULE_LIST 7 + +static SCHEDULE_LIST gScheduleList[MAX_SCHEDULE_LIST]; +static int g_isUpdate = 0; +static int g_startSeconds = -1; +static int g_durationSeconds = -1; +static int g_curWeekDay = -1; +static ScheduleCommand g_cmd = {0}; +static int g_oneTime = 0; + +static int GetWeekDay(unsigned char weekday, int Days[], int size) +{ + int n = 0; + if (size != MAX_SCHEDULE_LIST) { + SCH_ERR("weekday array is OOR! \n"); + return -1; + } + + for (int i = 0; i < MAX_SCHEDULE_LIST; i++) { + if ((weekday >> i) & 0x01) { + Days[n++] = i; + } + } + + return n; +} + +static void ScheduleGetStore(void) +{ + int total = ScheduleStoreGetTotal(); + if (total <= 0) { + SCH_ERR("no schedule stored! \n"); + return; + } + + for (int i = 0; i < total; i++) { + ScheduleInfo info = {0}; + int weeknum = 0; + int week[MAX_SCHEDULE_LIST] = {0}; + + if (ScheduleStoreGetInfo(&info, i) < 0) { + SCH_ERR("read schedulestore %d failed! \n", i); + continue; + } + + weeknum = GetWeekDay(info.week, week, MAX_SCHEDULE_LIST); + if (weeknum <= 0) { + SCH_ERR("the schedule has no weekday info! \n"); + continue; + } + + for (int j = 0; j < weeknum; j++) { + int weekday = week[j]; + if (weekday >= MAX_SCHEDULE_LIST) { + SCH_ERR("weekday is OOR!\n"); + break; + } + if (ScheduleListAppend(gScheduleList[weekday], &info) < 0) { + SCH_ERR("ScheduleListAppend failed!\n"); + break; + } + SCH_DBG("add info : id--%s, starttime--%d, duration--%d, week(%d)\n", + info.id, info.starttime, info.duration, weekday); + } + } +} + +int IOT_ScheduleInit(void) +{ + int retval = 0; + + for (int i = 0; i < MAX_SCHEDULE_LIST; i++) { + gScheduleList[i] = ScheduleListCreate(NULL); + if (gScheduleList[i] == NULL) { + SCH_ERR("ScheduleListCreate (%d) failed! \n", i); + retval = -1; + break; + } + } + + if (retval < 0) { + for (int i = 0; i < MAX_SCHEDULE_LIST; i++) { + if (gScheduleList[i] != NULL) { + ScheduleListDestroy(gScheduleList[i]); + gScheduleList[i] = NULL; + } + } + } else { + ScheduleGetStore(); + g_isUpdate = 1; + } + + return retval; +} + +static int ScheduleUpdate(const char *id, int *day, int size, int startTime, int durTime, + SCHEDULE_OPTION option, int cmd, int value) +{ +typedef int (*OptionFunc)(SCHEDULE_LIST mHandle, ScheduleInfo *info); + OptionFunc opFunc[] = {ScheduleListAppend, ScheduleListUpdate, ScheduleListDelete}; + ScheduleInfo info = {0}; + info.starttime = startTime; + info.duration = durTime; + info.scheduleCmd.cmd = cmd; + info.scheduleCmd.value = value; + + SCH_DBG("cmd = %d, value = %d \n", cmd, value); + + if (option > SCHEDULE_OPTION_DELETE) { + SCH_ERR("OPTION OOR!\n"); + return -1; + } + if (size == 1 && day[0] == 0) { // one time schedule + g_oneTime = 1; + g_isUpdate = 1; + g_startSeconds = startTime; + g_durationSeconds = durTime; + g_cmd.value = value; + g_cmd.cmd = cmd; + SCH_DBG("this schedule is onetime! \n"); + return 0; + } + + strncpy(info.id, id, strlen(id)); + + for (int i = 0; i < size; i++) { + int idx = day[i] - 1; + if (idx >= MAX_SCHEDULE_LIST) { + SCH_ERR("weekday is OOR! \n"); + continue; + } + info.week += (1 << idx); + + if (opFunc[option](gScheduleList[idx], &info) < 0) { + SCH_ERR("ScheduleListAppend failed! \n"); + return -1; + } + SCH_DBG("add info : id--%s, starttime--%d, duration--%d, week(%d)--0x%x, cmd--%d, value -- %d\n", + info.id, info.starttime, info.duration, idx, info.week, info.scheduleCmd.cmd, info.scheduleCmd.value); + } + + ScheduleStoreUpdate(&info, option); + + if ((option == SCHEDULE_OPTION_DELETE) && (g_startSeconds == startTime)) { + g_startSeconds = 0; + g_durationSeconds = 0; + } + + g_isUpdate = 1; + return 0; +} + +int IOT_ScheduleAdd(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value) +{ + return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_ADD, cmd, value); +} + +int IOT_ScheduleUpdate(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value) +{ + return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_UPDATE, cmd, value); +} + +int IOT_ScheduleDelete(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value) +{ + return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_DELETE, cmd, value); +} + +int IOT_ScheduleIsUpdate(unsigned int weekday, int curtime) +{ + int total = 0; + int retval = 0; + + if (g_isUpdate == 0) { + return 0; + } + + g_isUpdate = 0; + + if (g_oneTime) { + g_oneTime = 0; + if (g_startSeconds > curtime) { + SCH_DBG("get one time schedule! \n"); + return 1; + } + } + + + if (weekday >= MAX_SCHEDULE_LIST || curtime < 0) { + return 0; + } + + total = ScheduleListGetSize(gScheduleList[weekday]); + if (total <= 0) { + return 0; + } + + for (int i = 0; i < total; i++) { + ScheduleInfo info; + if (ScheduleListGet(gScheduleList[weekday], i, (ScheduleInfo *)&info) < 0) { + SCH_ERR("ScheduleListGet failed! \n"); + return 0; + } + + if (curtime < (info.starttime + info.duration)) { + if (g_curWeekDay == weekday) { + if (g_startSeconds != info.starttime || + g_durationSeconds != info.duration) { + g_startSeconds = info.starttime; + g_durationSeconds = info.duration; + g_cmd.cmd = info.scheduleCmd.cmd; + g_cmd.value = info.scheduleCmd.value; + retval = 1; + } + } else { + g_curWeekDay = weekday; + g_startSeconds = info.starttime; + g_durationSeconds = info.duration; + g_cmd.cmd = info.scheduleCmd.cmd; + g_cmd.value = info.scheduleCmd.value; + retval = 1; + } + break; + } + } + + if (retval == 1) { + SCH_DBG("cmd = %d, value = %d \n", g_cmd.cmd, g_cmd.value); + } + + return retval; +} + +void IOT_ScheduleSetUpdate(int update) +{ + g_isUpdate = update; +} + +int IOT_ScheduleGetStartTime(void) +{ + return g_startSeconds; +} + +int IOT_ScheduleGetDurationTime(void) +{ + return g_durationSeconds; +} + +int IOT_ScheduleGetCommand(int *cmd, int *value) +{ + if (cmd != NULL) { + *cmd = g_cmd.cmd; + } + + if (value != NULL) { + *value = g_cmd.value; + } + + return 0; +} + +void IOT_ScheduleDeinit(void) +{ + for (int i = 0; i < MAX_SCHEDULE_LIST; i++) { + if (gScheduleList[i] != NULL) { + ScheduleListDestroy(gScheduleList[i]); + gScheduleList[i] = NULL; + } + } +} diff --git a/common/iot_schedule_posix/schedule_list.c b/common/iot_schedule_posix/schedule_list.c new file mode 100644 index 0000000000000000000000000000000000000000..15207a0f44b071cd1c2ac16c4608369cf402122e --- /dev/null +++ b/common/iot_schedule_posix/schedule_list.c @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "schedule_list.h" + +typedef struct __ScheduleNode { + ScheduleInfo *data; + struct __ScheduleNode *prev; + struct __ScheduleNode *next; +} ScheduleNode; + +typedef struct { + ScheduleNode *head, *tail; + int total; +} ScheduleList; + +static ScheduleNode *ScheduleNodeCreate(ScheduleInfo *info) +{ + ScheduleNode *pNode; + + if (info == NULL) { + SCH_ERR("NULL POINT! \n"); + return NULL; + } + + pNode = (ScheduleNode *)malloc(sizeof(ScheduleNode)); + if (pNode == NULL) { + SCH_ERR("OOM!\n"); + return NULL; + } + + pNode->data = (char *)malloc(sizeof(ScheduleInfo)); + if (pNode->data == NULL) { + SCH_ERR("OOM!\n"); + free(pNode); + return NULL; + } + + memcpy(pNode->data, info, sizeof(ScheduleInfo)); + pNode->prev = NULL; + pNode->next = NULL; + + return pNode; +} + +static void ScheduleNodeDestroy(ScheduleNode *pNode) +{ + if (pNode == NULL) { + return; + } + if (pNode->data) { + free(pNode->data); + pNode->data = NULL; + } + free(pNode); + pNode = NULL; +} + +static ScheduleNode *ScheduleGetNodeWithTime(ScheduleNode *head, int time) +{ + ScheduleNode *pNode = head; + while (pNode) { + if (pNode->data && (time <= pNode->data->starttime)) { + break; + } + pNode = pNode->next; + } + + return pNode; +} + +static ScheduleNode *ScheduleGetNodeWithId(ScheduleNode *head, const char *id) +{ + ScheduleNode *pNode = head; + if (id == NULL) { + return NULL; + } + while (pNode) { + if (pNode->data && (strcmp(pNode->data->id, id) == 0)) { + break; + } + pNode = pNode->next; + } + + return pNode; +} + +static ScheduleNode *ScheduleGetNodeWithIdx(ScheduleNode *head, int idx) +{ + ScheduleNode *pNode = head; + for (int i = 0; i < idx; i++) { + if (pNode) { + pNode = pNode->next; + } + } + + return pNode; +} + +SCHEDULE_LIST ScheduleListCreate(ScheduleInfo *info) +{ + ScheduleNode *pNode; + ScheduleList *list = (ScheduleList *)malloc(sizeof(ScheduleList)); + if (list == NULL) { + SCH_ERR("OOM!\n"); + return NULL; + } + + pNode = ScheduleNodeCreate(info); + if (pNode != NULL) { + list->total = 1; + } else { + list->total = 0; + } + list->head = list->tail = pNode; + + return (SCHEDULE_LIST)list; +} + +int ScheduleListAppend(SCHEDULE_LIST mHandle, ScheduleInfo *info) +{ + ScheduleNode *pNode; + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL || info == NULL) { + SCH_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ScheduleNodeCreate(info); + if (pNode == NULL) { + SCH_ERR("ScheduleNodeCreate failed! \n"); + return -1; + } + + if (list->head == NULL) { + list->head = list->tail = pNode; + } else { + ScheduleNode *temp = ScheduleGetNodeWithTime(list->head, info->starttime); + if (temp == NULL) { + pNode->prev = list->tail; + list->tail->next = pNode; + list->tail = pNode; + } else { + pNode->prev = temp->prev; + pNode->next = temp; + if (temp->prev != NULL) { + temp->prev->next = pNode; + } else { + list->head = pNode; + } + temp->prev = pNode; + } + } + + list->total++; + + return 0; +} + +int ScheduleListUpdate(SCHEDULE_LIST mHandle, ScheduleInfo *info) +{ + int retval = 0; + ScheduleNode *pNode; + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL || info == NULL) { + SCH_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ScheduleGetNodeWithId(list->head, info->id); + if (pNode != NULL) { + memcpy(pNode->data, info, sizeof(ScheduleInfo)); + } else { + retval = ScheduleListAppend(mHandle, info); + } + + return retval; +} + +int ScheduleListDelete(SCHEDULE_LIST mHandle, ScheduleInfo *info) +{ + ScheduleNode *pNode, *prev, *next; + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL || info == NULL) { + SCH_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ScheduleGetNodeWithId(list->head, info->id); + if (pNode == NULL) { + return -1; + } + prev = pNode->prev; + next = pNode->next; + + if (prev == NULL && next == NULL) { + list->head = list->tail = NULL; + } else if (prev == NULL && next != NULL) { + next->prev = NULL; + list->head = next; + } else if (prev != NULL && next == NULL) { + prev->next = NULL; + list->tail = prev; + } else { + prev->next = next; + next->prev = prev; + } + pNode->next = NULL; + pNode->prev = NULL; + list->total--; + ScheduleNodeDestroy(pNode); + + return 0; +} + +int ScheduleListGetSize(SCHEDULE_LIST mHandle) +{ + if (mHandle == NULL) { + return -1; + } + + return ((ScheduleList *)mHandle)->total; +} + +int ScheduleListGet(SCHEDULE_LIST mHandle, int idx, ScheduleInfo *info) +{ + ScheduleNode *pNode; + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL || info == NULL) { + SCH_ERR("NULL POINT! \n"); + return -1; + } + + pNode = ScheduleGetNodeWithIdx(list->head, idx); + if (pNode == NULL || pNode->data == NULL) { + SCH_ERR("no such node(%d) \n", idx); + return -1; + } + + memcpy(info, pNode->data, sizeof(ScheduleInfo)); + + return 0; +} + +void ScheduleListDestroy(SCHEDULE_LIST mHandle) +{ + ScheduleList *list = (ScheduleList *)mHandle; + if (list == NULL) { + return; + } + while (list->head) { + ScheduleNode *pNode = list->head; + list->head = pNode->next; + pNode->next = NULL; + list->head->prev = NULL; + + ScheduleNodeDestroy(pNode); + } + list->total = 0; + + free(list); + list = NULL; +} diff --git a/common/iot_schedule_posix/schedule_store.c b/common/iot_schedule_posix/schedule_store.c new file mode 100644 index 0000000000000000000000000000000000000000..784e866d739029bccecb95fc0b834ce3ac32c3e2 --- /dev/null +++ b/common/iot_schedule_posix/schedule_store.c @@ -0,0 +1,287 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "ohos_errno.h" +#include "ohos_types.h" + +#include "schedule_store.h" + +#define SCHEDULE_KEY "/data/shcedule_info" +#define TEMP_KEY "/data/info.tmp" + +static int SearchScheduleInfo(const char *id) +{ + int idx = -1; + int total = 0; + int file; + + if (id == NULL) { + return -1; + } + + file = open(SCHEDULE_KEY, O_RDWR); + if (file < 0) { + return -1; + } + + lseek(file, 0, SEEK_SET); + if (read(file, (char *)&total, sizeof(int)) < 0) { + close(file); + return -1; + } + + for (int i = 0; i < total; i++) { + ScheduleInfo info; + int off = sizeof(ScheduleInfo) * i + sizeof(int); + + lseek(file, off, SEEK_SET); + + if (read(file, (char *)&info, sizeof(ScheduleInfo)) < 0) { + close(file); + return -1; + } + + if (strncmp((const char *)info.id, id, strlen(id)) == 0) { + idx = i; + break; + } + } + + close(file); + + return idx; +} + +static int DeletScheduleStore(ScheduleInfo *info) +{ + int fd; + int tmp; + int idx; + int total; + if (info == NULL) { + return -1; + } + + idx = SearchScheduleInfo((const char *)info->id); + if (idx < 0) { + return -1; + } + + fd = open(SCHEDULE_KEY, O_RDWR); + if (fd < 0) { + return -1; + } + + lseek(fd, 0, SEEK_SET); + if (read(fd, (char *)&total, sizeof(int)) < 0) { + close(fd); + return -1; + } + if (total <= 0) { + close(fd); + return -1; + } else if (total == 1) { + close(fd); + unlink(SCHEDULE_KEY); + return 0; + } + + total -= 1; + + tmp = open(TEMP_KEY, O_CREAT | O_RDWR, 0); + if (tmp < 0) { + close(fd); + return -1; + } + + if (write(tmp, (char *)&total, sizeof(int)) < 0) { + close(fd); + close(tmp); + return -1; + } + + for (int i = 0; i <= total; i++) { + ScheduleInfo tmpInfo; + if (read(fd, (char *)&tmpInfo, sizeof(ScheduleInfo)) < 0) { + close(fd); + close(tmp); + return -1; + } + if (i == idx) { + continue; + } + if (write(tmp, (char *)&tmpInfo, sizeof(ScheduleInfo)) < 0) { + close(fd); + close(tmp); + return -1; + } + } + + close(fd); + close(tmp); + + unlink(SCHEDULE_KEY); + rename(TEMP_KEY, SCHEDULE_KEY); + + return 0; +} + +static int UpdateScheduleStore(ScheduleInfo *info) +{ + int fd, idx; + + if (info == NULL) { + return -1; + } + + idx = SearchScheduleInfo((const char *)info->id); + if (idx < 0) { + return -1; + } + + fd = open(SCHEDULE_KEY, O_RDWR); + if (fd < 0) { + return -1; + } + + if (lseek(fd, idx * sizeof(ScheduleInfo) + sizeof(int), SEEK_SET) < 0) { + close(fd); + return -1; + } + + if (write(fd, (char *)info, sizeof(ScheduleInfo)) < 0) { + close(fd); + return -1; + } + + close(fd); + + return 0; +} + +static int AddScheduleStore(ScheduleInfo *info) +{ + int total = 0; + int fd; + + if (info == NULL) { + return -1; + } + + fd = open(SCHEDULE_KEY, O_RDWR | O_CREAT | O_APPEND); + if (fd < 0) { + return -1; + } + lseek(fd, 0, SEEK_SET); + if (read(fd, (char *)&total, sizeof(int)) < 0) { + close(fd); + return -1; + } + total += 1; + lseek(fd, 0, SEEK_SET); + if (write(fd, (char *)&total, sizeof(int)) < 0) { + close(fd); + return -1; + } + + lseek(fd, 0, SEEK_END); + if (write(fd, (char *)info, sizeof(ScheduleInfo)) < 0) { + close(fd); + return -1; + } + + close(fd); + + return 0; +} + +int ScheduleStoreUpdate(ScheduleInfo *info, SCHEDULE_OPTION option) +{ + int retval = -1; + switch (option) { + case SCHEDULE_OPTION_ADD: + AddScheduleStore(info); + break; + case SCHEDULE_OPTION_UPDATE: + UpdateScheduleStore(info); + break; + case SCHEDULE_OPTION_DELETE: + DeletScheduleStore(info); + break; + default: + break; + } + +} + +int ScheduleStoreGetTotal(void) +{ + int total = 0; + int fd = open(SCHEDULE_KEY, O_RDWR); + if (fd < 0) { + return -1; + } + + if (read(fd, (char *)&total, sizeof(int)) < 0) { + total = -1; + } + close(fd); + + return total; +} + +int ScheduleStoreGetInfo(ScheduleInfo *info, int idx) +{ + int total = 0; + int fd = open(SCHEDULE_KEY, O_RDWR); + if (fd < 0) { + return -1; + } + + if (read(fd, (char *)&total, sizeof(int)) < 0) { + close(fd); + return -1; + } + + if (idx >= total) { + close(fd); + return -1; + } + + if (lseek(fd, idx * sizeof(ScheduleInfo) + sizeof(int), SEEK_SET) < 0) { + close(fd); + return -1; + } + + if (read(fd, (char *)info, sizeof(ScheduleInfo)) < 0) { + close(fd); + return -1; + } + + close(fd); + + return 0; +} + +void ScheduleStoreDelete(void) +{ + unlink(SCHEDULE_KEY); +} diff --git a/common/iot_sntp/BUILD.gn b/common/iot_sntp/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..aec721d3ba2eb391814b752790339ae78802d7b7 --- /dev/null +++ b/common/iot_sntp/BUILD.gn @@ -0,0 +1,28 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_sntp") { + sources = [ + "iot_sntp.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "../inc", + ] + deps = [ + ] +} diff --git a/common/iot_sntp/iot_sntp.c b/common/iot_sntp/iot_sntp.c new file mode 100644 index 0000000000000000000000000000000000000000..5045b69e6de763216861a775df9df86b98065186 --- /dev/null +++ b/common/iot_sntp/iot_sntp.c @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iot_sntp.h" +#include +#include +#include +#include +#include +#include +#include + +#include "ohos_init.h" +#include "ohos_types.h" +#include "cmsis_os2.h" + +#include "lwip/sockets.h" +#include "kv_store.h" + +// this is the default SNTP server and port defines, you could define your own by add it in the config.gni +#ifndef NTP_SERVER_IP +#define NTP_SERVER_IP "120.24.166.46" +#endif +#ifndef NTP_SERVER_PORT +#define NTP_SERVER_PORT 123 +#endif + +/** + * The ntp server returns the time from 1900-1-1-0-0-0, and the unix + * timestamp compute from 1970-1-1-0-0-0, so we define the seconds between + * 1900-1-1-0-0-0 and 1970-1-1-0-0-0; when we get timestamp from NTP server, + * we must sub this time + */ +#define SECONDS_FROM_1900_TO_1970 (2208988800L) + +/** + * @brief NTP procto packet message + * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | LI| VN |Mode | Stratum | Poll | Precision | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | Root delay (32Bits) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | Root dispersion (32Bits) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | Reference identifier (32Bits) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | | + * | Reference timestamp (64Bits) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | | + * | Originate timestamp (64Bits) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | | + * | Receive timestamp (64Bits) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | | + * | Transmit timestamp (64Bits) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | | + * | Authenticator(optional 96Bits) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * + * 主要字段的解释如下: + * LI:当前时间闰秒标志。字段长度为2位整数,只在服务器端有效。取值定义为: + * LI=0:无警告; + * LI=1:最后一分钟是61秒; + * LI=2:最后一分钟是59秒; + * LI=3:警告(时钟没有同步) + * 服务器在开始时,LI设置为3,一旦与主钟取得同步后就设置成其它值。 + * VN(Version Number):长度为3比特,表示NTP的版本号,可以是3或者是4 + * Mode:指示协议模式。字段长度为3位,取值定义为: + * Mode=0:保留 + * Mode=1:对称主动; + * Mode=2:对称被动; + * Mode=3:客户; + * Mode=4:服务器; + * Mode=5:广播; + * Mode=6:保留为NTP控制信息; + * Mode=7:保留为用户定义; + * 在单播和多播模式,客户在请求时把这个字段设置为3,服务器在响应时把这个字段设置为4。在广播模式下,服务器把这个字段设置为5。 + * Poll Interval:指示数据包的最大时间间隔,以秒为单位,作为2的指数方的指数部分,该字段只在服务器端有效。字段长度为8位整数,取值范围从4-17,即16秒到131,072秒。 + * Precision:指示系统时钟的精确性,以秒为单位,作为2的指数方的指数部分,该字段只在服务器端有效。字段长度为8位符号整数,取值范围从-6到-20。 + * Root Delay:指示与主时钟参考源的总共往返延迟,以秒为单位,该字段只在服务器端有效。字段长度为32位浮点数,小数部分在16位以后,取值范围从负几毫秒到正几百毫秒。 + * Root Dispersion:指示与主时钟参考源的误差,以秒为单位,该字段只在服务器端有效。字段长度为32位浮点数,小数部分在16位以后,取值范围从零毫秒到正几百毫秒。 + * Reference Identifier:指示时钟参考源的标记,该字段只在服务器端有效。对于一级服务器,字段长度为4字节ASCII字符串,左对齐不足添零。对于二级服务器,在IPV4环境下,取值为一级服务器的IP地址,在IPV6环境下,是一级服务器的NSAP地址。 + * Reference Timestamp:指示系统时钟最后一次校准的时间,该字段只在服务器端有效,以前面所述64位时间戳格式表示。 + * Originate Timestamp:指示客户向服务器发起请求的时间,以前面所述64位时间戳格式表示。 + * Receive Timestamp:指服务器收到客户请求的时间 ,以前面所述64位时间戳格式表示。 + * Transmit Timestamp:指示服务器向客户发时间戳的时间,以前面所述64位时间戳格式表示。 + * Authenticator(可选):当需要进行SNTP认证时,该字段包含密钥和信息加密码 + * + */ + +/** + * @brief defines the NTP packet corresponding to the procto + * + */ +#pragma pack(1) +typedef struct { + uint32_t low; + uint32_t high; +}TimeStamp; // for the 64Bits timestamp use + +typedef struct { + uint8_t liVnMode; + uint8_t stratum; + uint8_t poll; + uint8_t precision; + uint32_t rootDelay; + uint32_t rootDispersion; + uint8_t referenceID[4]; + TimeStamp refecenceTimestamp; + TimeStamp originateTimestamp; + TimeStamp receiveTimestamp; + TimeStamp transmitTimestamp; +}SntpPacket; +#pragma pack() + +#define CN_TIMEZONE_MAXIMUM (11) +#define CN_TIMEZONE_MINIMUM (-11) +#define CN_SNTP_RETRYTIMES 10 + +int SntpGetRtcTime(int localTimeZone, struct tm *rtcTime) +{ + int ret = -1; + int sockfd = -1; + struct sockaddr_in server; + socklen_t addrLen; + fd_set set; + struct timeval timeout; + struct timeval tv; + int nRet; + SntpPacket sntpPacket; + struct tm *time = NULL; + int tryTimes = CN_SNTP_RETRYTIMES; + + // check the params passed to this API + if ((localTimeZone > CN_TIMEZONE_MAXIMUM) || (localTimeZone < CN_TIMEZONE_MINIMUM) ||\ + rtcTime == NULL) { + return ret; + } + + // prepare the server address + bzero(&server, sizeof(server)); + server.sin_family = AF_INET; + server.sin_port = htons(NTP_SERVER_PORT); + addrLen = sizeof(server); + if (inet_aton(NTP_SERVER_IP, &server.sin_addr) <= 0) { + printf("inet_pton error\n"); + return ret; + } + + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + return ret; + } + + // we do the loop here to get the time + do{ + tryTimes--; + // prepare the send packet + bzero((char * )&sntpPacket, sizeof(sntpPacket)); // clean it + sntpPacket.liVnMode = 0x1b; // set the mode to client + if (sendto(sockfd, (char *) &sntpPacket, sizeof(sntpPacket), 0, + (struct sockaddr*) &server, addrLen) < 0) { + printf("sendto error!\n"); + continue; + } + // use select to get the data + FD_ZERO(&set); + FD_SET(sockfd, &set); + timeout.tv_sec = 0; + timeout.tv_usec = 500000; + nRet = select(sockfd + 1, &set, NULL, NULL, &timeout); + if (nRet < 0) { + printf("select error!\n"); + continue; + } else if (nRet == 0) { + printf("time out!\n"); + continue; + } else { + if (FD_ISSET(sockfd, &set)) { + if (recvfrom(sockfd, (char *) &sntpPacket, sizeof(sntpPacket), + 0, (struct sockaddr*) &server, &addrLen) < 0) { + printf("recv error!\n"); + continue; + } else { + tv.tv_usec = 0; + tv.tv_sec = ntohl(sntpPacket.transmitTimestamp.low); + tv.tv_sec -= SECONDS_FROM_1900_TO_1970; + tv.tv_sec += (localTimeZone * 3600); + if((time = gmtime_r((const long int *) &tv.tv_sec, rtcTime)) != NULL) { + ret = 0; + break; + } + } + } + } + } while (tryTimes > 0); + closesocket(sockfd); + + return ret; +} \ No newline at end of file diff --git a/common/iot_socket/BUILD.gn b/common/iot_socket/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..173e9638214fda4fbcebbe443f58e4af5b1ca508 --- /dev/null +++ b/common/iot_socket/BUILD.gn @@ -0,0 +1,28 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_socket") { + sources = [ + "socket_client.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "../inc", + ] + deps = [ + ] +} diff --git a/common/iot_socket/socket_client.c b/common/iot_socket/socket_client.c new file mode 100644 index 0000000000000000000000000000000000000000..6a847426b3dbd4d2e3a7fccb9a494a4772510bcf --- /dev/null +++ b/common/iot_socket/socket_client.c @@ -0,0 +1,304 @@ + +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "socket_client.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ohos_types.h" + +#define SOCKET_TASK_STACK (1024 * 8) +#define SOCKET_TASK_PERIOD 27 + +#define UDP_DEF_PORT 9090 +#define TCP_DEF_PORT 8989 + +#define MSG_DATE_BUF_LEN 24 +#define PROTOCOL_HEADER "HM" +#define DATA_DEVICE_NAME "TD" +#define DATA_SWITCH "TA" +#define DATA_MSG_ON "on" +#define DATA_MSG_OFF "off" + +#ifndef ARRAYSIZE +#define ARRAYSIZE(a) (sizeof((a)) / sizeof((a[0]))) +#endif +#ifndef bool +#define bool unsigned char +#endif +#ifndef true +#define true 1 +#endif +#ifndef false +#define false 0 +#endif + +#define SOCKET_CLIENT_DEBUG +#ifdef SOCKET_CLIENT_DEBUG +#define SCK_ERR(fmt, args...) printf("[SOCKET_ERROR][%s|%d]" fmt, __func__, __LINE__, ##args) +#define SCK_DBG(fmt, args...) printf("[SOCKET_DEBUG][%s|%d]" fmt, __func__, __LINE__, ##args) +#define SCK_INFO(fmt, args...) printf("[SOCKET_INFO][%s|%d]" fmt, __func__, __LINE__, ##args) +#else +#define SCK_ERR(fmt, args...) do {} while(0) +#define SCK_DBG(fmt, args...) do {} while(0) +#define SCK_INFO(fmt, args...) do {} while(0) +#endif + +static void ResolveDevName(SocketEventCallback callback, char *value); +static void ResolveSwitch(SocketEventCallback callback, char *value); + +typedef union { + char msg[MSG_DATE_BUF_LEN]; + struct { + char head[2]; + char cmd[2]; + char buff[20]; + } msg_info; +} MsgInfo; + +typedef struct { + char cmd[MSG_DATE_BUF_LEN]; + void (*func)(SocketEventCallback callback, char *value); +} MsgData; + +static MsgData g_msgData[] = { + {DATA_DEVICE_NAME, ResolveDevName}, + {DATA_SWITCH, ResolveSwitch} +}; + +static bool g_threadRunning = false; + +// ********************************************************************************************************************************** // +static bool IsEqualTo(const char *msg1, const char *msg2, int length) +{ + if (msg1 == NULL || msg2 == NULL || length <= 0) { + SCK_ERR("NULL POINT! \n"); + return false; + } + + return (strncasecmp(msg1, msg2, length) == 0); +} + +static void ResolveDevName(SocketEventCallback callback, char *value) +{ + SCK_INFO(" ########### value : %s ################ \n", value); +} + +static void ResolveSwitch(SocketEventCallback callback, char *value) +{ + SCK_INFO(" ########### value : %s ################ \n", value); + if (callback != NULL) { + callback(SOCKET_SET_CMD, value); + } +} + +static int SocketClientResolveData(const char *data, int len, SocketEventCallback callback) +{ + MsgInfo msgInfo = {0}; + if (data == NULL || len <= 0) { + SCK_ERR("NULL POINT!\n"); + return -1; + } + + if (len > MSG_DATE_BUF_LEN) { + len = MSG_DATE_BUF_LEN; + } + + memcpy(msgInfo.msg, data, len); + SCK_DBG("head:%s\n", msgInfo.msg_info.head); + for (int i = 0; i < ARRAYSIZE(g_msgData); i++) { + if (IsEqualTo(msgInfo.msg_info.cmd, g_msgData[i].cmd, strlen(g_msgData[i].cmd))) { + g_msgData[i].func(callback, msgInfo.msg_info.buff); + SCK_INFO(" cmd %s is match! \n", g_msgData[i].cmd); + break; + } + } + + return 0; +} + +static int SocketOpen(const char *ip, int port) +{ + int sockfd; + struct sockaddr_in recvAddr; + if (ip == NULL || port <= 0) { + return -1; + } + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + SCK_ERR("socket failed! errno=%d\n", errno); + return -1; + } + + memset(&recvAddr, 0x00, sizeof(recvAddr)); + recvAddr.sin_family = AF_INET; + recvAddr.sin_port = htons(port); + recvAddr.sin_addr.s_addr = inet_addr(ip); + + if (connect(sockfd, (struct sockaddr *)&recvAddr, sizeof(recvAddr)) < 0) { + SCK_ERR("connect failed! errno=%d\n", errno); + close(sockfd); + return -1; + } + + return sockfd; +} + +static int GetServerIp(char *ip, int size) +{ + char recMsg[256] = {0}; + char sockfd; + char *tmp = NULL; + struct sockaddr_in localAddr, serverAddr; + int sockaddr_len = sizeof(serverAddr); + + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + SCK_ERR("socket failed!\n"); + return -1; + } + + memset(&localAddr, 0x00, sizeof(localAddr)); + localAddr.sin_family = AF_INET; + localAddr.sin_port = htons(UDP_DEF_PORT); + localAddr.sin_addr.s_addr = inet_addr(INADDR_ANY); + + if (bind(sockfd, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0) { + SCK_ERR("bind failed! errno : %d(%s)\n", errno, strerror(errno)); + close(sockfd); + return -1; + } + + if (recvfrom(sockfd, recMsg, sizeof(recMsg), 0, (struct sockaddr *)&serverAddr, &sockaddr_len) < 0) { + SCK_ERR("recvfrom failed! \n"); + close(sockfd); + return -1; + } + + tmp = inet_ntoa(serverAddr.sin_addr); + SCK_INFO("the server ip is %s \n", tmp); + if (ip == NULL || size < strlen(tmp)) { + SCK_ERR("params is invalid!! \n"); + close(sockfd); + return -1; + } + + strncpy(ip, tmp, strlen(tmp)); + + close(sockfd); + + return 0; +} + +static void SocketClientProcess(void *arg) +{ + int sockfd = -1; + char ipBuf[256] = {0}; + char sendBuf[256] = {0}; + char mDeviceName[256] = {0}; + + SocketCallback *mCallback = (SocketCallback *)arg; + if (mCallback == NULL) { + SCK_ERR("socket callback is NULL! \n"); + return; + } + + if (GetServerIp(ipBuf, sizeof(ipBuf)) < 0) { + SCK_ERR("get server ip failed! \n"); + g_threadRunning = false; + return; + } + + sockfd = SocketOpen((const char *)ipBuf, TCP_DEF_PORT); + if (sockfd < 0) { + SCK_ERR("socket open failed! \n"); + g_threadRunning = false; + return; + } + + if (mCallback->socketEvent != NULL) { + mCallback->socketEvent(SOCKET_CONNECTTED, NULL); + } + + if (mCallback->socketGetDeviceName != NULL) { + mCallback->socketGetDeviceName(mDeviceName, sizeof(mDeviceName)); + sprintf(sendBuf, "%s%s%s", PROTOCOL_HEADER, DATA_DEVICE_NAME, mDeviceName); + if (send(sockfd, sendBuf, strlen(sendBuf), 0) < 0) { + SCK_ERR("send %s failed! \n", sendBuf); + goto EXIT; + } + } + + while (g_threadRunning) { + char recvBuf[1024] = {0}; + int recvBytes = recv(sockfd, recvBuf, sizeof(recvBuf), 0); + if (recvBytes <= 0) { + break; + } + SCK_INFO("recvMsg[%d] : %s \n", recvBytes, recvBuf); + if (SocketClientResolveData((const char *)recvBuf, recvBytes, mCallback->socketEvent) < 0) { + break; + } + } + +EXIT: + close(sockfd); + sockfd = -1; + if (mCallback->socketEvent != NULL) { + mCallback->socketEvent(SOCKET_DISCONNECT, NULL); + } + g_threadRunning = false; +} + +int SocketClientStart(SocketCallback *gCallback) +{ + osThreadAttr_t attr; + + attr.name = "SocketClientTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = SOCKET_TASK_STACK; + attr.priority = SOCKET_TASK_PERIOD; + + g_threadRunning = true; + + if (osThreadNew(SocketClientProcess, (void *)gCallback, &attr) == NULL) { + SCK_ERR("Falied to create ClientTask!\n"); + return -1; + } + + return 0; +} + +void SocketClientStop(void) +{ + if (g_threadRunning) { + g_threadRunning = false; + } +} diff --git a/common/iot_socket/socket_client.h b/common/iot_socket/socket_client.h new file mode 100644 index 0000000000000000000000000000000000000000..c1ba420cf5ee6d32b04fdc4b6e6c5c69aca47274 --- /dev/null +++ b/common/iot_socket/socket_client.h @@ -0,0 +1,39 @@ + +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __SOCKET_CLIENT_H__ +#define __SOCKET_CLIENT_H__ + +typedef enum { + SOCKET_CONNECTTED = 1, + SOCKET_DISCONNECT, + SOCKET_SET_CMD +} SOCKET_EVENT; + +typedef int (*SocketGetDeviceName)(char *buf, int size); + +typedef int (*SocketEventCallback)(SOCKET_EVENT event, void *value); + +typedef struct { + SocketGetDeviceName socketGetDeviceName; + SocketEventCallback socketEvent; +} SocketCallback; + +int SocketClientStart(SocketCallback *gCallback); + +void SocketClientStop(void); + +#endif /* __SOCKET_CLIENT_H__ */ diff --git a/common/iot_wifi/BUILD.gn b/common/iot_wifi/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9946985b477d9a290569e227d9f46795b494e821 --- /dev/null +++ b/common/iot_wifi/BUILD.gn @@ -0,0 +1,48 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +config_file = [ + "libs/libnetcfgdevicesdk.a", + "libs/libhilinkadapter_3861.a" +] + +copy("config") { + sources = config_file + outputs = [ "$root_build_dir/libs/{{source_file_part}}" ] +} + +static_library("iot_wifi") { + sources = [ + "iot_wifi.c", + "iot_softap.c", + "iot_netcfg.c", + "iot_netcfg_nan.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//foundation/communication/wifi_lite/interfaces/wifiservice", + "//third_party/mbedtls/include/mbedtls", + "//kernel/liteos_m/kal/cmsis", + "//third_party/cJSON", + "../inc", + ] + + deps = [ + + ] + +} diff --git a/common/iot_wifi/iot_netcfg.c b/common/iot_wifi/iot_netcfg.c new file mode 100644 index 0000000000000000000000000000000000000000..e5080a6be9a87e1834e414f6d514c8e2a5ccf2e4 --- /dev/null +++ b/common/iot_wifi/iot_netcfg.c @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "errno.h" +#include "cmsis_os2.h" +#include "sys/time.h" +#include "sys/socket.h" +#include "netinet/in.h" +#include "lwip/inet.h" +#include "lwip/netdb.h" + +#include "cJSON.h" +#include "ohos_types.h" +#include "iot_softap.h" +#include "iot_netcfg.h" +#include "iot_demo_def.h" + +#define SSID_MAX_LEN 32 +#define PWD_MAX_LEN 32 +#define NET_PORT 8686 +#define RETRY_TIMES 5 +#define CONNECT_TIMEOUT 20 +#define CMD_KEY "cmd" +#define PAR_KEY "param" +#define SID_KEY "wifiName" +#define PWD_KEY "wifiPassword" +#define CMD_CONFIG 0x20 +#define REQUEST_STR "{\"cmd\":32,\"code\":200,\"msg\":\"\"}" + +static int ParseWifiInfo(const char *psr, char *ssid, int sl, char *pwd, int pl) +{ + cJSON *json; + cJSON *sub; + cJSON *obj; + int cmd; + char *ps = NULL; + char *pw = NULL; + + json = cJSON_Parse(psr); + RaiseLog(LOG_LEVEL_ERR, "parse_json: %s\n", psr); + if (json == NULL) { + RaiseLog(LOG_LEVEL_ERR, "parse_json: %s\n", psr); + return -1; + } + + sub = cJSON_GetObjectItem(json, CMD_KEY); + cmd = cJSON_GetNumberValue(sub); + if (cmd != CMD_CONFIG) { + RaiseLog(LOG_LEVEL_ERR, "CMD TYPE FAILED! cmd=%d \n", cmd); + cJSON_Delete(json); + return -1; + } + + sub = cJSON_GetObjectItem(json, PAR_KEY); + if (sub == NULL) { + RaiseLog(LOG_LEVEL_ERR, "get param obj failed! \n"); + cJSON_Delete(json); + return -1; + } + + obj = cJSON_GetObjectItem(sub, SID_KEY); + ps = cJSON_GetStringValue(obj); + if (ps == NULL) { + RaiseLog(LOG_LEVEL_ERR, "get ssid failed! \n"); + cJSON_Delete(json); + return -1; + } + + if (strncpy_s(ssid, sl, ps, strlen(ps)) < 0) { + RaiseLog(LOG_LEVEL_ERR, "strncpy_s failed! \n"); + cJSON_Delete(json); + return -1; + } + + obj = cJSON_GetObjectItem(sub, PWD_KEY); + pw = cJSON_GetStringValue(obj); + if (pw != NULL) { + RaiseLog(LOG_LEVEL_INFO, "pw=%s\n", pw); + if (strncpy_s(pwd, pl, pw, strlen(pw)) < 0) { + RaiseLog(LOG_LEVEL_ERR, "strncpy_s failed! \n"); + cJSON_Delete(json); + return -1; + } + } + + cJSON_Delete(json); + return 0; +} + +static int NetCfgGetSocketValue(int sockfd, char *ssid, int sLen, char *pwd, int pLen) +{ + int result = -1; + + if (sockfd < 0) { + RaiseLog(LOG_LEVEL_ERR, "sockfd invalid!\n"); + return -1; + } + + if (ssid == NULL || sLen < SSID_MAX_LEN || pwd == NULL || pLen < PWD_MAX_LEN) { + RaiseLog(LOG_LEVEL_ERR, "NULL POINT!\n"); + return -1; + } + + while (1) { + int readbytes; + struct sockaddr_in addr; + char recvbuf[BUFF_SIZE] = {0}; + socklen_t len = sizeof(addr); + + readbytes = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&addr, &len); + if (readbytes <= 0) { + RaiseLog(LOG_LEVEL_ERR, "socket disconnect! \n"); + break; + } + + if (ParseWifiInfo((const char *)recvbuf, ssid, sLen, pwd, pLen) == 0) { + sendto(sockfd, REQUEST_STR, strlen(REQUEST_STR), 0, (struct sockaddr *)&addr, &len); + result = 0; + break; + } + } + + return result; +} + +static int NetCfgGetWifiInfo(char *ssid, int sLen, char *pwd, int pLen) +{ + int sockfd; + struct sockaddr_in servaddr; + + if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { + RaiseLog(LOG_LEVEL_ERR, "socket error! \n"); + return -1; + } + + memset_s(&servaddr, sizeof(servaddr), 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(NET_PORT); + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + RaiseLog(LOG_LEVEL_INFO, "listen port %d\n", NET_PORT); + if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + RaiseLog(LOG_LEVEL_ERR, "bind socket! \n"); + close(sockfd); + return -1; + } + + if (listen(sockfd, 1) < 0) { + RaiseLog(LOG_LEVEL_ERR,"listen failed! errno = %d \n", errno); + close(sockfd); + return -1; + } + + while (1) { + int newfd, retval; + struct sockaddr_in client_addr; + socklen_t length = sizeof(client_addr); + newfd = accept(sockfd, (struct sockaddr *)&client_addr, &length); + RaiseLog(LOG_LEVEL_INFO, "new socket connect!\n"); + retval = NetCfgGetSocketValue(newfd, ssid, sLen, pwd, pLen); + close(newfd); + + if (retval == 0) { + RaiseLog(LOG_LEVEL_INFO, "config network success! \n"); + break; + } + } + + close(sockfd); + + return 0; +} + +int BOARD_NetCfgStartConfig(const char *appName, char *ssid, int sLen, char *pwd, int pLen) +{ + int retval = 0; + + if (BOARD_SoftApStart(appName) < 0) { + RaiseLog(LOG_LEVEL_ERR, "start softap failed! \n"); + return -1; + } + + if (NetCfgGetWifiInfo(ssid, sLen, pwd, pLen) < 0) { + RaiseLog(LOG_LEVEL_ERR, "start softap failed! \n"); + retval = -1; + } + + BOARD_SoftApStop(); + + return retval; +} \ No newline at end of file diff --git a/common/iot_wifi/iot_netcfg_nan.c b/common/iot_wifi/iot_netcfg_nan.c new file mode 100644 index 0000000000000000000000000000000000000000..ebf94f0602a3c078c607dc404ecb94f34ca916eb --- /dev/null +++ b/common/iot_wifi/iot_netcfg_nan.c @@ -0,0 +1,435 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "cmsis_os2.h" +#include "base64.h" +#include "ohos_types.h" +#include "lwip/netif.h" +#include "lwip/netifapi.h" +#include "lwip/ip4_addr.h" +#include "wifi_hotspot.h" +#include "wifi_device_config.h" +#include "iot_netcfg_nan.h" +#include "network_config_service.h" +#include "iot_demo_def.h" + +WifiDeviceConfig g_netCfg = {0}; +WifiEvent g_staEventHandler = {0}; +struct netif *g_staNetif = NULL; + +int g_connectRetryCount = 0; + + +static NetCfgEventCallback g_netCfgEventCallback = NULL; + +#define SET_NET_EVENT(e, d) ({ \ + if (g_netCfgEventCallback != NULL) { \ + g_netCfgEventCallback(e, d); \ + } \ +}) + +char g_ssid[SSID_MAX_LEN + 1] = {0}; +const char *g_pinCode = "11111111"; +const char *g_productId = "1"; +const char *g_sn = "01234567890123450123456789012345"; + +static unsigned int ChannelToFrequency(unsigned int channel) +{ + if (channel <= 0) { + return 0; + } + + if (channel == CHANNEL_80211B_ONLY) { + return FREQ_OF_CHANNEL_80211B_ONLY; + } + + return (((channel - WIFI_MIN_CHANNEL) * WIFI_FREQ_INTERVAL) + FREQ_OF_CHANNEL_1); +} + +static void NetCfgResult(signed char result) +{ + RaiseLog(LOG_LEVEL_INFO, "Network configure done.(result=%d)\n", result); + UnRegisterWifiEvent(&g_staEventHandler); + NotifyNetCfgResult(result); +} + +static void StaResetAddr(struct netif *ptrLwipNetif) +{ + ip4_addr_t staGW; + ip4_addr_t staIpaddr; + ip4_addr_t staNetmask; + + if (ptrLwipNetif == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Null param of netdev\r\n"); + return; + } + + IP4_ADDR(&staGW, 0, 0, 0, 0); + IP4_ADDR(&staIpaddr, 0, 0, 0, 0); + IP4_ADDR(&staNetmask, 0, 0, 0, 0); + + netifapi_netif_set_addr(ptrLwipNetif, &staIpaddr, &staNetmask, &staGW); +} + +static int g_state; + +static void *WifiConnectTask(const char *arg) +{ + (void)arg; + if (g_state == WIFI_STATE_AVALIABLE) { + NetCfgResult(0); + RaiseLog(LOG_LEVEL_INFO, "WiFi: Connected.\n"); + netifapi_dhcp_start(g_staNetif); + } else if (g_state == WIFI_STATE_NOT_AVALIABLE) { + RaiseLog(LOG_LEVEL_INFO, "WiFi: Disconnected retry = %d\n", g_connectRetryCount); + if (g_connectRetryCount < TEST_CONNECT_RETRY_COUNT) { + g_connectRetryCount++; + return NULL; + } + NetCfgResult(-1); + netifapi_dhcp_stop(g_staNetif); + StaResetAddr(g_staNetif); + } + return NULL; +} + +static void WifiConnectionChangedHandler(int state, WifiLinkedInfo *info) +{ + (void)info; + osThreadAttr_t attr; + attr.name = "WifiConnectTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = NETCFG_TASK_STACK_SIZE; + attr.priority = NETCFG_TASK_PRIO; + g_state = state; + if (osThreadNew((osThreadFunc_t)WifiConnectTask, NULL, &attr) == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Falied to create WifiConnectTask!\n"); + } +} + +static int StaStart(void) +{ + WifiErrorCode error; + error = EnableWifi(); + if (error == ERROR_WIFI_BUSY) { + RaiseLog(LOG_LEVEL_ERR, "Sta had already connnected.\n"); + NetCfgResult(0); + } + if ((error != ERROR_WIFI_BUSY) && (error != WIFI_SUCCESS)) { + RaiseLog(LOG_LEVEL_ERR, "EnableWifi failed, error = %d\n", error); + return -1; + } + + g_staEventHandler.OnWifiConnectionChanged = WifiConnectionChangedHandler; + error = RegisterWifiEvent(&g_staEventHandler); + if (error != WIFI_SUCCESS) { + RaiseLog(LOG_LEVEL_ERR, "RegisterWifiEvent failed, error = %d\n", error); + return -1; + } + + if (IsWifiActive() == 0) { + RaiseLog(LOG_LEVEL_ERR, "Wifi station is not actived.\n"); + return -1; + } + + g_staNetif = netif_find("wlan0"); + if (g_staNetif == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Get netif failed\n"); + return -1; + } + + return 0; +} + +static int WapStaConnect(WifiDeviceConfig *config) +{ + int netId = 0; + WifiErrorCode error; + config->securityType = (config->preSharedKey[0] == '\0') ? WIFI_SEC_TYPE_OPEN : WIFI_SEC_TYPE_PSK; + error = AddDeviceConfig(config, &netId); + if (error != WIFI_SUCCESS) { + RaiseLog(LOG_LEVEL_ERR, "AddDeviceConfig add config failed, error=%d\n", error); + return -1; + } + int count = 0; + while (count < FAST_CONNECT_RETRY_NUM) { + error = ConnectTo(netId); + if (error == WIFI_SUCCESS) { + break; + } + RaiseLog(LOG_LEVEL_INFO, "[sample]continue\n"); + count++; + } + + if (error != WIFI_SUCCESS) { + RaiseLog(LOG_LEVEL_ERR, "ConnectTo conn failed %d\n", error); + return -1; + } + + RaiseLog(LOG_LEVEL_INFO, "WapSta connecting...\n"); + return 0; +} + +static void *CfgNetTask(const char *arg) +{ + (void)arg; + + if (StaStart() != 0) { + return NULL; + } + + if (WapStaConnect(&g_netCfg) != 0) { + return NULL; + } + + return NULL; +} + +static int CreateCfgNetTask(void) +{ + osThreadAttr_t attr; + attr.name = "CfgNetTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = NETCFG_TASK_STACK_SIZE; + attr.priority = NETCFG_TASK_PRIO; + + if (osThreadNew((osThreadFunc_t)CfgNetTask, NULL, &attr) == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Falied to create NanCfgNetTask!\n"); + return -1; + } + + return 0; +} + +static void DealSsidPwd(const WifiDeviceConfig *config) +{ + if (config == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Input config is illegal.\n"); + return; + } + + if (memcpy_s(&g_netCfg, sizeof(WifiDeviceConfig), config, sizeof(WifiDeviceConfig)) != 0) { + RaiseLog(LOG_LEVEL_ERR, "memcpy netCfg failed.\n"); + return; + } + RaiseLog(LOG_LEVEL_INFO, "DealSsidPwd\n"); +} + +int GetPinCode(unsigned char *pinCode, unsigned int size, unsigned int *len) +{ + if (pinCode == NULL) { + return -1; + } + memset_s(pinCode, size, 0, size); + if (strncpy_s((char *)pinCode, size, g_pinCode, strlen(g_pinCode)) != 0) { + RaiseLog(LOG_LEVEL_ERR, "GetPinCode copy pinCode failed\n"); + return -1; + } + *len = strlen((char *)pinCode); + return 0; +} + +int FastConnect(const struct WifiInfo *wifiInfo, WifiDeviceConfig *destCfg) +{ + if (memcpy_s(destCfg->ssid, sizeof(destCfg->ssid), wifiInfo->ssid, wifiInfo->ssidLen) != EOK) { + RaiseLog(LOG_LEVEL_ERR, "FastConnect copy ssid failed\n"); + return -1; + } + if (memcpy_s(destCfg->preSharedKey, sizeof(destCfg->preSharedKey), wifiInfo->psk, wifiInfo->pskLen) != EOK) { + RaiseLog(LOG_LEVEL_ERR, "FastConnect copy pwd failed\n"); + return -1; + } + if (memcpy_s(destCfg->bssid, sizeof(destCfg->bssid), wifiInfo->bssid, wifiInfo->bssidLen) != EOK) { + RaiseLog(LOG_LEVEL_ERR, "FastConnect copy bssid failed\n"); + return -1; + } + destCfg->securityType = wifiInfo->authMode; + destCfg->freq = ChannelToFrequency(wifiInfo->channelNumber); + destCfg->wapiPskType = WIFI_PSK_TYPE_HEX; + return 0; +} + +int ParseNetCfgData(const struct WifiInfo *wifiInfo, const unsigned char *vendorData, unsigned int len) +{ + RaiseLog(LOG_LEVEL_INFO, "ParseWifiData vendorData len:%d\n", len); + if (wifiInfo == NULL) { + RaiseLog(LOG_LEVEL_ERR, "wifiInfo is NULL\n"); + return -1; + } + + if (vendorData != NULL) { + /* process vendorData */ + } + + if (memcpy_s(g_netCfg.ssid, sizeof(g_netCfg.ssid), wifiInfo->ssid, wifiInfo->ssidLen) != EOK) { + RaiseLog(LOG_LEVEL_ERR, "Copy ssid failed\n"); + return -1; + } + if (memcpy_s(g_netCfg.preSharedKey, sizeof(g_netCfg.preSharedKey), wifiInfo->pwd, wifiInfo->pwdLen) != EOK) { + RaiseLog(LOG_LEVEL_ERR, "Copy pwd failed\n"); + return -1; + } + + return 0; +} + +int SendRawEncodeData(const unsigned char *data, size_t len) +{ + size_t writeLen = 0; + int ret = mbedtls_base64_encode(NULL, 0, &writeLen, (const unsigned char *)data, len); + if (ret != 0) { + RaiseLog(LOG_LEVEL_ERR, "SendRawEncodeData base64 encode fial\n"); + return -1; + } + size_t encodeBufLen = writeLen; + if (writeLen > MAX_DATA_LEN) { + RaiseLog(LOG_LEVEL_ERR, "SendRawEncodeData dataLen overSize\n"); + return -1; + } + char *buf = malloc(writeLen + 1); + if (buf == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Malloc failed\r\n"); + return -1; + } + (void)memset_s(buf, writeLen + 1, 0, writeLen + 1); + if (mbedtls_base64_encode((unsigned char *)buf, encodeBufLen, &writeLen, (const unsigned char *)data, len) != 0) { + RaiseLog(LOG_LEVEL_ERR, "SendRawEncodeData base64 encode failed\r\n"); + free(buf); + buf = NULL; + return -1; + } + + LOG_I("SendRawEncodeData encode buf = %s\n", buf); + SendRawData((const char*)buf); + if (buf != NULL) { + free(buf); + } + + return 0; +} + +void NotifyNetCfgStatus(enum NetCfgStatus status) +{ + (void)status; + return; +} + +static int RecvRawData(const char *svcId, unsigned int mode, const char *data) +{ + (void)svcId; + (void)mode; + + RaiseLog(LOG_LEVEL_DEBUG, "data : %s \n\n", data); + return 0; +} + +static void *NetCfgTask(void *arg) +{ + (void)arg; + int ret; + ret = SetSafeDistancePower(POWER_NUM); // Make sure the device discovery distance about 30 cm + if (ret != 0) { + RaiseLog(LOG_LEVEL_ERR, "Set saft distance power failed\n"); + return NULL; + } + + struct SoftAPParam config = {0}; + memset_s(&config, sizeof(struct SoftAPParam), 0, sizeof(struct SoftAPParam)); + strncpy_s(config.ssid, sizeof(config.ssid), g_ssid, strlen(g_ssid)); + config.authType = WIFI_SECURITY_OPEN; + ret = SetSoftAPParameter(&config); + if (ret != 0) { + RaiseLog(LOG_LEVEL_ERR, "Set softAP parameters failed\n"); + return NULL; + } + + NetCfgCallback hook; + memset_s(&hook, sizeof(NetCfgCallback), 0, sizeof(NetCfgCallback)); + hook.GetPinCode = GetPinCode; + hook.ParseNetCfgData = ParseNetCfgData; + hook.RecvRawData = RecvRawData; + hook.NotifyNetCfgStatus = NotifyNetCfgStatus; + ret = RegNetCfgCallback(&hook); + if (ret != 0) { + RaiseLog(LOG_LEVEL_ERR, "Register config callback failed\n"); + return NULL; + } + + struct DevInfo devInfo[DEVICE_INFO_NUM]; + memset_s(&devInfo, sizeof(devInfo), 0, sizeof(devInfo)); + devInfo[0].key = "productId"; + devInfo[1].key = "sn"; + devInfo[0].value = g_productId; + devInfo[1].value = g_sn; + ret = StartNetCfg(devInfo, DEVICE_INFO_NUM, NETCFG_SOFTAP_NAN); + if (ret != 0) { + RaiseLog(LOG_LEVEL_ERR, "Start config wifi fail.\n"); + return NULL; + } + + return NULL; +} + +void NetCfgRegister(void) +{ + RaiseLog(LOG_LEVEL_INFO, "NetCfgRegister enter.\n"); + osThreadAttr_t attr; + + attr.name = "NetCfgTask_NAN"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = NETCFG_TASK_STACK_SIZE; + attr.priority = NETCFG_TASK_PRIO; + + if (osThreadNew((osThreadFunc_t)NetCfgTask, NULL, &attr) == NULL) { + RaiseLog(LOG_LEVEL_ERR, "Falied to create NetCfgTask!\n"); + } +} + +int BOARD_NAN_NetCfgStartConfig(const char *apName, char *ssid, int sLen, char *pwd, int pLen) +{ + if (memcpy_s(g_ssid, sizeof(g_ssid), apName, strlen(apName)) != EOK) { + RaiseLog(LOG_LEVEL_ERR, "copy apName failed\n"); + return -1; + } + + NetCfgRegister(); + + while (strlen(g_netCfg.ssid) == 0) { + osDelay(100); + } + + RaiseLog(LOG_LEVEL_DEBUG, "g_netCfg.ssid :%s g_netCfg.preSharedKey :%s\n", g_netCfg.ssid, g_netCfg.preSharedKey); + if (memcpy_s(ssid, sLen, g_netCfg.ssid, sizeof(g_netCfg.ssid)) != EOK) { + RaiseLog(LOG_LEVEL_ERR, "copy ssid failed\n"); + return -1; + } + + if (memcpy_s(pwd, pLen, g_netCfg.preSharedKey, sizeof(g_netCfg.preSharedKey)) != EOK) { + RaiseLog(LOG_LEVEL_ERR, "copy pwd failed\n"); + return -1; + } + RaiseLog(LOG_LEVEL_DEBUG, "ssid :%s pwd :%s\n", ssid, pwd); + return 0; +} \ No newline at end of file diff --git a/common/iot_wifi/iot_softap.c b/common/iot_wifi/iot_softap.c new file mode 100644 index 0000000000000000000000000000000000000000..c29bbf53e8fb27e87c1f5ddde45fdc0ca4ad4ebc --- /dev/null +++ b/common/iot_wifi/iot_softap.c @@ -0,0 +1,109 @@ + +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "lwip/ip_addr.h" +#include "lwip/netifapi.h" +#include "wifi_hotspot.h" +#include "wifi_hotspot_config.h" +#include "iot_softap.h" +#include "iot_demo_def.h" + +#define DEFAULT_AP_NAME "TestIotAP" + +static struct netif *g_netif = NULL; + +static void SetAddr(struct netif *pst_lwip_netif) +{ + ip4_addr_t st_gw; + ip4_addr_t st_ipaddr; + ip4_addr_t st_netmask; + + IP4_ADDR(&st_ipaddr, 192, 168, 10, 1); // IP ADDR + IP4_ADDR(&st_gw, 192, 168, 10, 1); // GET WAY ADDR + IP4_ADDR(&st_netmask, 255, 255, 255, 0); // NET MASK CODE + + netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw); +} + +static void ResetAddr(struct netif *pst_lwip_netif) +{ + ip4_addr_t st_gw; + ip4_addr_t st_ipaddr; + ip4_addr_t st_netmask; + + IP4_ADDR(&st_ipaddr, 0, 0, 0, 0); + IP4_ADDR(&st_gw, 0, 0, 0, 0); + IP4_ADDR(&st_netmask, 0, 0, 0, 0); + + netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw); +} + +int BOARD_SoftApStart(const char *ap_name) +{ + HotspotConfig config = {0}; + ip4_addr_t st_gw; + ip4_addr_t st_ipaddr; + ip4_addr_t st_netmask; + char *apName = ap_name; + char ifname[BUFF_SIZE] = {0}; + int retval; + + if (IsHotspotActive() == WIFI_HOTSPOT_ACTIVE) { + RaiseLog(LOG_LEVEL_ERR, "WIFI_HOTSPOT_ACTIVE \n"); + return -1; + } + if (apName == NULL) { + apName = DEFAULT_AP_NAME; + } + if (strcpy_s(config.ssid, sizeof(config.ssid), apName) != 0) { + RaiseLog(LOG_LEVEL_ERR, "[sample] strcpy ssid fail.\n"); + return -1; + } + config.securityType = WIFI_SEC_TYPE_OPEN; + retval = SetHotspotConfig((const HotspotConfig *)(&config)); + if (retval != WIFI_SUCCESS) { + RaiseLog(LOG_LEVEL_ERR, "SetHotspotConfig \n"); + return -1; + } + + retval = EnableHotspot(); + if (retval != WIFI_SUCCESS) { + RaiseLog(LOG_LEVEL_ERR, "EnableHotspot failed! \n"); + return -1; + } + + g_netif = netifapi_netif_find("ap0"); + if (g_netif != NULL) { + SetAddr(g_netif); + dhcps_start(g_netif, NULL, 0); + } + + return 0; +} + +void BOARD_SoftApStop(void) +{ + if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) { + RaiseLog(LOG_LEVEL_ERR, "WIFI_HOTSPOT_NOT_ACTIVE \n"); + return; + } + + DisableHotspot(); + if (g_netif) { + ResetAddr(g_netif); + } +} diff --git a/common/iot_wifi/iot_wifi.c b/common/iot_wifi/iot_wifi.c new file mode 100644 index 0000000000000000000000000000000000000000..b09654116ee4baffa9f909bf8c9c0ff39b4b5058 --- /dev/null +++ b/common/iot_wifi/iot_wifi.c @@ -0,0 +1,259 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "lwip/netif.h" +#include "lwip/netifapi.h" +#include "lwip/ip4_addr.h" +#include "lwip/api_shell.h" +#include "cmsis_os2.h" +#include "wifi_device.h" +#include "wifi_error_code.h" +#include "ohos_init.h" +#include "iot_wifi.h" + +#define DEF_TIMEOUT 15 +#define ONE_SECOND 1 +#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK + +static void WiFiInit(void); +static void WaitSacnResult(void); +static int WaitConnectResult(void); +static void OnWifiScanStateChangedHandler(int state, int size); +static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info); +static void OnHotspotStaJoinHandler(StationInfo *info); +static void OnHotspotStateChangedHandler(int state); +static void OnHotspotStaLeaveHandler(StationInfo *info); + +static int g_staScanSuccess = 0; +static int g_ConnectSuccess = 0; +static int ssid_count = 0; +WifiEvent g_wifiEventHandler = {0}; +WifiErrorCode error; + +#define SELECT_WLAN_PORT "wlan0" + +int WifiConnect(const char *ssid, const char *psk) +{ + WifiScanInfo *info = NULL; + unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT; + static struct netif *g_lwip_netif = NULL; + + osDelay(200); + printf("<--System Init-->\r\n"); + + // 初始化WIFI + WiFiInit(); + + // 使能WIFI + if (EnableWifi() != WIFI_SUCCESS) { + printf("EnableWifi failed, error = %d\r\n", error); + return -1; + } + + // 判断WIFI是否激活 + if (IsWifiActive() == 0) + { + printf("Wifi station is not actived.\r\n"); + return -1; + } + + // 分配空间,保存WiFi信息 + info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT); + if (info == NULL) { + return -1; + } + // 轮询查找WiFi列表 + do { + // 重置标志位 + ssid_count = 0; + g_staScanSuccess = 0; + + // 开始扫描 + Scan(); + + // 等待扫描结果 + WaitSacnResult(); + + // 获取扫描列表 + error = GetScanInfoList(info, &size); + + } while (g_staScanSuccess != 1); + + for (uint8_t i = 0; i < ssid_count; i++) { + printf("no:%03d, ssid:%-30s, rssi:%5d\r\n", i+1, info[i].ssid, info[i].rssi/100); + } + + // 连接指定的WiFi热点 + for (uint8_t i = 0; i < ssid_count; i++) { + if (strcmp(ssid, info[i].ssid) == 0) { + int result; + printf("Select:%3d wireless, Waiting...\r\n", i+1); + // 拷贝要连接的热点信息 + WifiDeviceConfig select_ap_config = {0}; + strcpy(select_ap_config.ssid, info[i].ssid); + strcpy(select_ap_config.preSharedKey, psk); + select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE; + + if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS) { + if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1) { + printf("WiFi connect succeed!\r\n"); + g_lwip_netif = netifapi_netif_find(SELECT_WLAN_PORT); + break; + } + } + } + + if (i == ssid_count - 1) { + printf("ERROR: No wifi as expected\r\n"); + while(1) osDelay(100); + } + } + // 启动DHCP + if (g_lwip_netif) { + dhcp_start(g_lwip_netif); + printf("begain to dhcp\r\n"); + } + + // 等待DHCP + for(;;) { + if(dhcp_is_bound(g_lwip_netif) == ERR_OK) { + printf("<-- DHCP state:OK -->\r\n"); + // 打印获取到的IP信息 + netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL); + break; + } + printf("<-- DHCP state:Inprogress -->\r\n"); + osDelay(100); + } + + osDelay(100); + + return 0; +} + +/**************************************************** +* 初始化WIFI +****************************************************/ +static void WiFiInit(void) +{ + printf("<--Wifi Init-->\r\n"); + g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler; + g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler; + g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler; + g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler; + g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler; + error = RegisterWifiEvent(&g_wifiEventHandler); + if (error != WIFI_SUCCESS) { + printf("register wifi event fail!\r\n"); + } else { + printf("register wifi event succeed!\r\n"); + } +} + +static void OnWifiScanStateChangedHandler(int state, int size) +{ + if (size > 0) { + ssid_count = size; + g_staScanSuccess = 1; + } + printf("callback function for wifi scan:%d, %d\r\n", state, size); + return; +} + +static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info) +{ + if (info == NULL) { + printf("WifiConnectionChanged:info is null, stat is %d.\n", state); + } else { + if (state == WIFI_STATE_AVALIABLE) { + g_ConnectSuccess = 1; + } else { + g_ConnectSuccess = 0; + } + } +} + +static void OnHotspotStaJoinHandler(StationInfo *info) +{ + (void)info; + printf("STA join AP\n"); + return; +} + +static void OnHotspotStaLeaveHandler(StationInfo *info) +{ + (void)info; + printf("HotspotStaLeave:info is null.\n"); + return; +} + +static void OnHotspotStateChangedHandler(int state) +{ + printf("HotspotStateChanged:state is %d.\n", state); + return; +} + +static void WaitSacnResult(void) +{ + int scanTimeout = DEF_TIMEOUT; + while (scanTimeout > 0) { + sleep(ONE_SECOND); + scanTimeout--; + if (g_staScanSuccess == 1) { + printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout)); + break; + } + } + if (scanTimeout <= 0) { + printf("WaitSacnResult:timeout!\n"); + } +} + +static int WaitConnectResult(void) +{ + int ConnectTimeout = DEF_TIMEOUT; + while (ConnectTimeout > 0) { + sleep(ONE_SECOND); + ConnectTimeout--; + if (g_ConnectSuccess == 1) { + printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout)); + break; + } + } + if (ConnectTimeout <= 0) { + printf("WaitConnectResult:timeout!\n"); + return 0; + } + return 1; +} + +int BOARD_InitWifi(void) +{ + return 0; +} + +int BOARD_ConnectWifi(const char *wifiSSID, const char *wifiPWD) +{ + return WifiConnect(wifiSSID, wifiPWD); +} + +int BOARD_DisconnectWifi() +{ + Disconnect(); + return 0; +} diff --git a/common/iot_wifi/libs/libhilinkadapter_3861.a b/common/iot_wifi/libs/libhilinkadapter_3861.a new file mode 100644 index 0000000000000000000000000000000000000000..5590f0a020adc05afc40fda24a936127cfec5f9b Binary files /dev/null and b/common/iot_wifi/libs/libhilinkadapter_3861.a differ diff --git a/common/iot_wifi/libs/libnetcfgdevicesdk.a b/common/iot_wifi/libs/libnetcfgdevicesdk.a new file mode 100644 index 0000000000000000000000000000000000000000..c76f38e6cf26b57aa9f660ea71fd7de66e6a513c Binary files /dev/null and b/common/iot_wifi/libs/libnetcfgdevicesdk.a differ diff --git a/common/iot_wifi_bes2600/BUILD.gn b/common/iot_wifi_bes2600/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..7ba13bbb424488329d218edcb731ee619d15d6ff --- /dev/null +++ b/common/iot_wifi_bes2600/BUILD.gn @@ -0,0 +1,44 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("iot_wifi") { + + sources = [ + "iot_wifi.c", + "iot_netcfg.c", + "iot_softap.c", + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + + include_dirs = [ + "//kernel/liteos_m/components/net/lwip-2.1/enhancement/include/lwip", + "//kernel/liteos_m/components/net/lwip-2.1/porting/include/", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", + "//foundation/communication/wifi_lite/interfaces/wifiservice/", + "//third_party/mbedtls/include/mbedtls", + "//kernel/liteos_m/kal/cmsis", + "//third_party/cJSON", + "//utils/native/lite/include", + "../inc", + ] + + deps = [ + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_static", + ] + + +} diff --git a/common/iot_wifi_bes2600/iot_bes2600_debug.h b/common/iot_wifi_bes2600/iot_bes2600_debug.h new file mode 100644 index 0000000000000000000000000000000000000000..a5e615d145030139be29798d3e10bf095eae3392 --- /dev/null +++ b/common/iot_wifi_bes2600/iot_bes2600_debug.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef IOT_BES2600_DEBUG_H_ +#define IOT_BES2600_DEBUG_H_ + + +#define LOG_TAG "BES2600_WIFI" +#include "hilog/hiview_log.h" + +#define LOG_E(fmt, ...) HILOG_ERROR(HILOG_MODULE_APP, fmt, ##__VA_ARGS__) +#define LOG_I(fmt, ...) HILOG_INFO(HILOG_MODULE_APP, fmt, ##__VA_ARGS__) + + +#endif /* __IOT_SCHEDULE_H__ */ \ No newline at end of file diff --git a/common/iot_wifi_bes2600/iot_netcfg.c b/common/iot_wifi_bes2600/iot_netcfg.c new file mode 100644 index 0000000000000000000000000000000000000000..fde79f8f8238f02b58ffff5282230d3c0d6bdda9 --- /dev/null +++ b/common/iot_wifi_bes2600/iot_netcfg.c @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +//#include "errno.h" +#include "cmsis_os2.h" +#include "sys/time.h" +#include "sys/socket.h" +#include "netinet/in.h" +#include "lwip/inet.h" +#include "lwip/netdb.h" + +#include "cJSON.h" +#include "ohos_types.h" +#include "iot_softap.h" +#include "iot_netcfg.h" +#include "iot_bes2600_debug.h" +#include "iot_demo_def.h" + +#define SSID_MAX_LEN 32 +#define PWD_MAX_LEN 32 +#define NET_PORT 8686 +#define RETRY_TIMES 5 +#define CONNECT_TIMEOUT 20 +#define CMD_KEY "cmd" +#define PAR_KEY "param" +#define SID_KEY "wifiName" +#define PWD_KEY "wifiPassword" +#define CMD_CONFIG 0x20 +#define REQUEST_STR "{\"cmd\":32,\"code\":200,\"msg\":\"\"}" + +static int ParseWifiInfo(const char *psr, char *ssid, int sl, char *pwd, int pl) +{ + cJSON *json; + cJSON *sub; + cJSON *obj; + int cmd; + char *ps = NULL; + char *pw = NULL; + + json = cJSON_Parse(psr); + LOG_E("parse_json: %s\n", psr); + if (json == NULL) { + LOG_E("parse_json: %s\n", psr); + return -1; + } + + sub = cJSON_GetObjectItem(json, CMD_KEY); + cmd = cJSON_GetNumberValue(sub); + if (cmd != CMD_CONFIG) { + LOG_E("CMD TYPE FAILED! cmd=%d \n", cmd); + cJSON_Delete(json); + return -1; + } + + sub = cJSON_GetObjectItem(json, PAR_KEY); + if (sub == NULL) { + LOG_E("get param obj failed! \n"); + cJSON_Delete(json); + return -1; + } + + obj = cJSON_GetObjectItem(sub, SID_KEY); + ps = cJSON_GetStringValue(obj); + if (ps == NULL) { + LOG_E("get ssid failed! \n"); + cJSON_Delete(json); + return -1; + } + + if (strncpy_s(ssid, sl, ps, strlen(ps)) < 0) { + LOG_E("strncpy_s failed! \n"); + cJSON_Delete(json); + return -1; + } + + obj = cJSON_GetObjectItem(sub, PWD_KEY); + pw = cJSON_GetStringValue(obj); + if (pw != NULL) { + LOG_I("pw=%s\n", pw); + if (strncpy_s(pwd, pl, pw, strlen(pw)) < 0) { + LOG_E("strncpy_s failed! \n"); + cJSON_Delete(json); + return -1; + } + } + + cJSON_Delete(json); + return 0; +} + +static int NetCfgGetSocketValue(int sockfd, char *ssid, int sLen, char *pwd, int pLen) +{ + int result = -1; + + if (sockfd < 0) { + LOG_E("sockfd invalid!\n"); + return -1; + } + + if (ssid == NULL || sLen < SSID_MAX_LEN || pwd == NULL || pLen < PWD_MAX_LEN) { + LOG_E("NULL POINT!\n"); + return -1; + } + + while (1) { + int readbytes; + struct sockaddr_in addr; + char recvbuf[BUFF_SIZE] = {0}; + socklen_t len = sizeof(addr); + + readbytes = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&addr, &len); + if (readbytes <= 0) { + LOG_E("socket disconnect! \n"); + break; + } + + if (ParseWifiInfo((const char *)recvbuf, ssid, sLen, pwd, pLen) == 0) { + sendto(sockfd, REQUEST_STR, strlen(REQUEST_STR), 0, (struct sockaddr *)&addr, &len); + result = 0; + break; + } + } + + return result; +} + +static int NetCfgGetWifiInfo(char *ssid, int sLen, char *pwd, int pLen) +{ + int sockfd; + struct sockaddr_in servaddr; + + if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { + LOG_E("socket error! \n"); + return -1; + } + + memset_s(&servaddr, sizeof(servaddr), 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(NET_PORT); + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + LOG_I("listen port %d\n", NET_PORT); + if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + LOG_E("bind socket! \n"); + close(sockfd); + return -1; + } + + if (listen(sockfd, 1) < 0) { + LOG_E("listen failed! errno = %d \n", errno); + close(sockfd); + return -1; + } + + while (1) { + int newfd, retval; + struct sockaddr_in client_addr; + socklen_t length = sizeof(client_addr); + newfd = accept(sockfd, (struct sockaddr *)&client_addr, &length); + LOG_I("new socket connect!\n"); + retval = NetCfgGetSocketValue(newfd, ssid, sLen, pwd, pLen); + close(newfd); + + if (retval == 0) { + LOG_I("config network success! \n"); + break; + } + } + + close(sockfd); + + return 0; +} + +int BOARD_NetCfgStartConfig(const char *appName, char *ssid, int sLen, char *pwd, int pLen) +{ + int retval = 0; + + if (BOARD_SoftApStart(appName) < 0) { + LOG_E("start softap failed! \n"); + return -1; + } + + if (NetCfgGetWifiInfo(ssid, sLen, pwd, pLen) < 0) { + LOG_E("start softap failed! \n"); + retval = -1; + } + + BOARD_SoftApStop(); + + return retval; +} \ No newline at end of file diff --git a/common/iot_wifi_bes2600/iot_softap.c b/common/iot_wifi_bes2600/iot_softap.c new file mode 100644 index 0000000000000000000000000000000000000000..f5334fe977d95d9c8e68fa84fe01ba09bb6f5feb --- /dev/null +++ b/common/iot_wifi_bes2600/iot_softap.c @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "lwip/tcpip.h" +#include "lwip/ip_addr.h" +#include "lwip/netifapi.h" +#include "dhcps.h" +#include "wifi_hotspot.h" +#include "wifi_hotspot_config.h" +#include "iot_softap.h" +#include "iot_bes2600_debug.h" + +#define DEFAULT_AP_NAME "TestIotAP" + +static unsigned char _mac_addr[6]; +extern struct netif if_wifi_ap; + +static void SetAddr(struct netif *pst_lwip_netif) +{ + ip4_addr_t st_gw; + ip4_addr_t st_ipaddr; + ip4_addr_t st_netmask; + + IP4_ADDR(&st_ipaddr, 192, 168, 51, 1); // IP ADDR 为了和其他数字管家中的其他设备适配, 选择192.168.51网段 + IP4_ADDR(&st_gw, 192, 168, 51, 1); // GET WAY ADDR + IP4_ADDR(&st_netmask, 255, 255, 255, 0); // NET MASK CODE + + netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw); +} + +static void ResetAddr(struct netif *pst_lwip_netif) +{ + ip4_addr_t st_gw; + ip4_addr_t st_ipaddr; + ip4_addr_t st_netmask; + + IP4_ADDR(&st_ipaddr, 0, 0, 0, 0); + IP4_ADDR(&st_gw, 0, 0, 0, 0); + IP4_ADDR(&st_netmask, 0, 0, 0, 0); + + netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw); +} + +int BOARD_SoftApStart(const char *ap_name) +{ + HotspotConfig config = {0}; + char *apName = ap_name; + int retval; + struct netif *p_netif = &if_wifi_ap; + + if (IsHotspotActive() == WIFI_HOTSPOT_ACTIVE) { + LOG_E("WIFI_HOTSPOT_ACTIVE \n"); + return -1; + } + if (apName == NULL) { + apName = DEFAULT_AP_NAME; + } + if (strcpy_s(config.ssid, sizeof(config.ssid), apName) != 0) { + LOG_E("[sample] strcpy ssid fail.\n"); + return -1; + } + config.securityType = WIFI_SEC_TYPE_OPEN; + retval = SetHotspotConfig((const HotspotConfig *)(&config)); + if (retval != WIFI_SUCCESS) { + LOG_E("SetHotspotConfig \n"); + return -1; + } + LOG_I("%s %d\r\n", __FUNCTION__, __LINE__); + retval = EnableHotspot(); + if (retval != WIFI_SUCCESS) { + LOG_E("EnableHotspot failed! \n"); + return -1; + } + + if (p_netif != NULL) { + LOG_I("Start dhcp server action%s %d\r\n", __FUNCTION__, __LINE__); + SetAddr(p_netif); // 设置网卡 IP信息 + dhcps_start(p_netif, NULL, 0); // 启动dhcp server + } + + return 0; +} + +void BOARD_SoftApStop(void) +{ + struct netif *p_netif = &if_wifi_ap; + if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) { + LOG_E("WIFI_HOTSPOT_NOT_ACTIVE \n"); + return; + } + + ResetAddr(p_netif); + dhcps_stop(p_netif); + DisableHotspot(); +} diff --git a/common/iot_wifi_bes2600/iot_wifi.c b/common/iot_wifi_bes2600/iot_wifi.c new file mode 100644 index 0000000000000000000000000000000000000000..1dba3268d8fa228e0d256e92ca940143ef015163 --- /dev/null +++ b/common/iot_wifi_bes2600/iot_wifi.c @@ -0,0 +1,261 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "lwip/netif.h" +#include "lwip/netifapi.h" +#include "lwip/ip4_addr.h" +#include "cmsis_os2.h" +#include "wifi_device.h" +#include "wifi_error_code.h" +#include "ohos_init.h" +#include "iot_wifi.h" +#include "iot_bes2600_debug.h" + +#define DEF_TIMEOUT 15 +#define ONE_SECOND 1 +#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK + +static void WiFiInit(void); +static void WaitSacnResult(void); +static int WaitConnectResult(void); +static void OnWifiScanStateChangedHandler(int state, int size); +static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info); +static void OnHotspotStaJoinHandler(StationInfo *info); +static void OnHotspotStateChangedHandler(int state); +static void OnHotspotStaLeaveHandler(StationInfo *info); + +static int g_staScanSuccess = 0; +static int g_ConnectSuccess = 0; +static int ssid_count = 0; +WifiEvent g_wifiEventHandler = {0}; +WifiErrorCode error; +extern struct netif if_wifi; + +#define SELECT_WLAN_PORT "wlan0" + +int WifiConnect(const char *ssid, const char *psk) +{ + WifiScanInfo *info = NULL; + unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT; + static struct netif *g_lwip_netif = NULL; + + osDelay(200); + LOG_I("<--System Init-->\r\n"); + + // 初始化WIFI + WiFiInit(); + + // 使能WIFI + if (EnableWifi() != WIFI_SUCCESS) { + LOG_E("EnableWifi failed, error = %d\r\n", error); + return -1; + } + + // 判断WIFI是否激活 + if (IsWifiActive() == 0) + { + LOG_E("Wifi station is not actived.\r\n"); + return -1; + } + + // 分配空间,保存WiFi信息 + info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT); + if (info == NULL) { + return -1; + } + // 轮询查找WiFi列表 + do { + // 重置标志位 + ssid_count = 0; + g_staScanSuccess = 0; + + // 开始扫描 + Scan(); + + // 等待扫描结果 + WaitSacnResult(); + + // 获取扫描列表 + error = GetScanInfoList(info, &size); + + } while (g_staScanSuccess != 1); + + for (uint8_t i = 0; i < ssid_count; i++) { + LOG_I("no:%03d, ssid:%-30s, rssi:%5d\r\n", i+1, info[i].ssid, info[i].rssi/100); + } + + // 连接指定的WiFi热点 + for (uint8_t i = 0; i < ssid_count; i++) { + if (strcmp(ssid, info[i].ssid) == 0) { + int result; + LOG_I("Select:%3d wireless, Waiting...\r\n", i+1); + // 拷贝要连接的热点信息 + WifiDeviceConfig select_ap_config = {0}; + strcpy(select_ap_config.ssid, info[i].ssid); + strcpy(select_ap_config.preSharedKey, psk); + select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE; + + if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS) { + if (ConnectTo(result) == WIFI_SUCCESS ) { + LOG_I("WiFi connect succeed!\r\n"); + g_lwip_netif = netifapi_netif_find_by_name(SELECT_WLAN_PORT); + LOG_I("g_lwip_netif.full_name %s!\r\n", g_lwip_netif->full_name); + LOG_I("g_lwip_netif.hostname %s!\r\n", g_lwip_netif->hostname); + + uint32_t ip = ((ip4_addr_t *)&g_lwip_netif->ip_addr)->addr; + uint32_t gw = ((ip4_addr_t *)&g_lwip_netif->gw)->addr; + uint32_t mask = ((ip4_addr_t *)&g_lwip_netif->netmask)->addr; + LOG_I("net_intf_status_change_cb **ip = %s\n", inet_ntoa(ip)); + LOG_I("net_intf_status_change_cb **netmask = %s\n", inet_ntoa(mask)); + LOG_I("net_intf_status_change_cb **gw = %s\n", inet_ntoa(gw)); + + LOG_I("WiFi connect succeed!\r\n"); + break; + } + } + } + + if (i == ssid_count - 1) { + LOG_E("ERROR: No wifi as expected\r\n"); + while(1) osDelay(100); + } + } + + // 等待DHCP + for(;;) { + if(dhcp_is_bound(g_lwip_netif) == ERR_OK) { + uint32_t ip = ((ip4_addr_t *)&g_lwip_netif->ip_addr)->addr; + uint32_t gw = ((ip4_addr_t *)&g_lwip_netif->gw)->addr; + uint32_t mask = ((ip4_addr_t *)&g_lwip_netif->netmask)->addr; + LOG_I("net_intf_status_change_cb **ip = %s\n", inet_ntoa(ip)); + LOG_I("net_intf_status_change_cb **netmask = %s\n", inet_ntoa(mask)); + LOG_I("net_intf_status_change_cb **gw = %s\n", inet_ntoa(gw)); + LOG_I("<-- DHCP state:OK -->\r\n"); + // 打印获取到的IP信息 + break; + } + LOG_I("<-- DHCP state:Inprogress -->\r\n"); + osDelay(100); + } + + osDelay(100); + + return 0; +} + +/**************************************************** +* 初始化WIFI +****************************************************/ +static void WiFiInit(void) +{ + LOG_I("<--Wifi Init-->\r\n"); + g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler; + //g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler; + g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler; + g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler; + g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler; + error = RegisterWifiEvent(&g_wifiEventHandler); + if (error != WIFI_SUCCESS) { + LOG_E("register wifi event fail!\r\n"); + } else { + LOG_I("register wifi event succeed!\r\n"); + } +} + +static void OnWifiScanStateChangedHandler(int state, int size) +{ + if (size > 0) { + ssid_count = size; + g_staScanSuccess = 1; + } + LOG_I("callback function for wifi scan:%d, %d\r\n", state, size); + return; +} + +static void OnHotspotStaJoinHandler(StationInfo *info) +{ + (void)info; + LOG_I("STA join AP\n"); + return; +} + +static void OnHotspotStaLeaveHandler(StationInfo *info) +{ + (void)info; + LOG_I("HotspotStaLeave:info is null.\n"); + return; +} + +static void OnHotspotStateChangedHandler(int state) +{ + LOG_I("HotspotStateChanged:state is %d.\n", state); + return; +} + +static void WaitSacnResult(void) +{ + int scanTimeout = DEF_TIMEOUT; + while (scanTimeout > 0) { + sleep(ONE_SECOND); + scanTimeout--; + if (g_staScanSuccess == 1) { + LOG_I("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout)); + break; + } + } + if (scanTimeout <= 0) { + LOG_I("WaitSacnResult:timeout!\n"); + } +} + +int BOARD_InitWifi(void) +{ + return 0; +} + +int BOARD_ConnectWifi(const char *wifiSSID, const char *wifiPWD) +{ + return WifiConnect(wifiSSID, wifiPWD); +} + +int BOARD_DisconnectWifi() +{ + Disconnect(); + return 0; +} + +#define RSSI_LEVEL_MAX (-55) +#define RSSI_LEVEL_MIN (-90) + +int BOARD_GetWifiSignalLevel(void) +{ + WifiLinkedInfo getLinkedRes; + if(WIFI_SUCCESS != GetLinkedInfo(&getLinkedRes)) { + LOG_E("%s %d: Get linked info faild!\r\n", __FUNCTION__, __LINE__); + return -1; + } + if(RSSI_LEVEL_MAX < getLinkedRes.rssi) { + return 4; + } + else if(RSSI_LEVEL_MIN > getLinkedRes.rssi) { + return 1; + } + else { + return (getLinkedRes.rssi / 15 + 7); + } +} diff --git a/common/iot_wifi_xradio/BUILD.gn b/common/iot_wifi_xradio/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..41f09e5f0779c5b58ecd3be30cfc86f8bab40fe3 --- /dev/null +++ b/common/iot_wifi_xradio/BUILD.gn @@ -0,0 +1,36 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +source_set("iot_wifi") { + sources = [ + "iot_netcfg.c", + "iot_softap_xradio.c", + "iot_wifi_xradio.c" + ] + + cflags = [ "-Wno-unused-variable" ] + cflags += [ "-Wno-unused-but-set-variable" ] + cflags += [ "-Wno-unused-parameter" ] + + include_dirs = [ + "//foundation/communication/wifi_lite/interfaces/wifiservice", + "//third_party/mbedtls/include/mbedtls", + "//kernel/liteos_m/kal/cmsis", + "//third_party/cJSON", + "../inc", + ] + + deps = [ + ] + +} diff --git a/common/iot_wifi_xradio/iot_netcfg.c b/common/iot_wifi_xradio/iot_netcfg.c new file mode 100644 index 0000000000000000000000000000000000000000..80975475f4a309bbeac9d32d876bd828a9c6e717 --- /dev/null +++ b/common/iot_wifi_xradio/iot_netcfg.c @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "errno.h" +#include "cmsis_os2.h" +#include "sys/time.h" +#include "lwip/inet.h" +#include "lwip/netdb.h" + +#include "cjson/cJSON.h" +#include "ohos_types.h" + +#if (defined(LOSCFG_SOC_COMPANY_ALLWINNER)) +#include "iot_softap_xradio.h" +#endif + +#include "iot_netcfg.h" +#include "iot_demo_def.h" + +#define SSID_MAX_LEN 32 +#define PWD_MAX_LEN 32 +#define NET_PORT 8686 +#define RETRY_TIMES 5 +#define CONNECT_TIMEOUT 20 +#define CMD_KEY "cmd" +#define PAR_KEY "param" +#define SID_KEY "wifiName" +#define PWD_KEY "wifiPassword" +#define CMD_CONFIG 0x20 +#define REQUEST_STR "{\"cmd\":32,\"code\":200,\"msg\":\"\"}" + +static int ParseWifiInfo(const char *psr, char *ssid, int sl, char *pwd, int pl) +{ + cJSON *json; + cJSON *sub = NULL; + cJSON *obj = NULL; + int cmd; + char *ps = NULL; + char *pw = NULL; + + json = cJSON_Parse(psr); + RaiseLog(LOG_LEVEL_ERR, "parse_json: %s\n", psr); + if (json == NULL) { + RaiseLog(LOG_LEVEL_ERR, "parse_json: %s\n", psr); + return -1; + } + + sub = cJSON_GetObjectItem(json, CMD_KEY); + cmd = sub->valueint; + if (cmd != CMD_CONFIG) { + RaiseLog(LOG_LEVEL_ERR, "CMD TYPE FAILED! cmd=%d \n", cmd); + cJSON_Delete(json); + return -1; + } + + sub = cJSON_GetObjectItem(json, PAR_KEY); + if (sub == NULL) { + RaiseLog(LOG_LEVEL_ERR, "get param obj failed! \n"); + cJSON_Delete(json); + return -1; + } + + obj = cJSON_GetObjectItem(sub, SID_KEY); + ps = obj->valuestring; + if (ps == NULL) { + RaiseLog(LOG_LEVEL_ERR, "get ssid failed! \n"); + cJSON_Delete(json); + return -1; + } + + if (strncpy_s(ssid, sl, ps, strlen(ps)) < 0) { + RaiseLog(LOG_LEVEL_ERR, "strncpy_s failed! \n"); + cJSON_Delete(json); + return -1; + } + + obj = cJSON_GetObjectItem(sub, PWD_KEY); + pw = obj->valuestring; + if (pw != NULL) { + RaiseLog(LOG_LEVEL_INFO, "pw=%s\n", pw); + if (strncpy_s(pwd, pl, pw, strlen(pw)) < 0) { + RaiseLog(LOG_LEVEL_ERR, "strncpy_s failed! \n"); + cJSON_Delete(json); + return -1; + } + } + + cJSON_Delete(json); + return 0; +} + +static int NetCfgGetSocketValue(int sockfd, char *ssid, int sLen, char *pwd, int pLen) +{ + int result = -1; + + if (sockfd < 0) { + RaiseLog(LOG_LEVEL_ERR, "sockfd invalid!\n"); + return -1; + } + + if (ssid == NULL || sLen < SSID_MAX_LEN || pwd == NULL || pLen < PWD_MAX_LEN) { + RaiseLog(LOG_LEVEL_ERR, "NULL POINT!\n"); + return -1; + } + + while (1) { + int readbytes; + struct sockaddr_in addr; + char recvbuf[BUFF_SIZE] = {0}; + socklen_t len = sizeof(addr); + + readbytes = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&addr, &len); + if (readbytes <= 0) { + RaiseLog(LOG_LEVEL_ERR, "socket disconnect! \n"); + break; + } + + if (ParseWifiInfo((const char *)recvbuf, ssid, sLen, pwd, pLen) == 0) { + sendto(sockfd, REQUEST_STR, strlen(REQUEST_STR), 0, (struct sockaddr *)&addr, &len); + result = 0; + break; + } + } + + return result; +} + +static int NetCfgGetWifiInfo(char *ssid, int sLen, char *pwd, int pLen) +{ + int sockfd; + struct sockaddr_in servaddr; + + if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { + RaiseLog(LOG_LEVEL_ERR, "socket error! \n"); + return -1; + } + + memset_s(&servaddr, sizeof(servaddr), 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(NET_PORT); + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + + RaiseLog(LOG_LEVEL_INFO, "listen port %d\n", NET_PORT); + if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + RaiseLog(LOG_LEVEL_ERR, "bind socket! \n"); + close(sockfd); + return -1; + } + + if (listen(sockfd, 1) < 0) { + RaiseLog(LOG_LEVEL_ERR, "listen failed! errno = %d \n", errno); + close(sockfd); + return -1; + } + + while (1) { + int newfd, retval; + struct sockaddr_in client_addr; + socklen_t length = sizeof(client_addr); + newfd = accept(sockfd, (struct sockaddr *)&client_addr, &length); + RaiseLog(LOG_LEVEL_INFO, "new socket connect!\n"); + retval = NetCfgGetSocketValue(newfd, ssid, sLen, pwd, pLen); + close(newfd); + + if (retval == 0) { + RaiseLog(LOG_LEVEL_INFO, "config network success! \n"); + break; + } + } + + close(sockfd); + + return 0; +} + +int BOARD_NetCfgStartConfig(const char *appName, char *ssid, int sLen, char *pwd, int pLen) +{ + int retval = 0; + + if (BOARD_SoftApStart(appName) < 0) { + RaiseLog(LOG_LEVEL_ERR, "start softap failed! \n"); + return -1; + } + + if (NetCfgGetWifiInfo(ssid, sLen, pwd, pLen) < 0) { + RaiseLog(LOG_LEVEL_ERR, "start softap failed! \n"); + retval = -1; + } + + BOARD_SoftApStop(); + + return retval; +} \ No newline at end of file diff --git a/common/iot_wifi_xradio/iot_softap_xradio.c b/common/iot_wifi_xradio/iot_softap_xradio.c new file mode 100644 index 0000000000000000000000000000000000000000..7da7aca13278f8b8d3098f5e1015fa97e21eb9bf --- /dev/null +++ b/common/iot_wifi_xradio/iot_softap_xradio.c @@ -0,0 +1,69 @@ + +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "lwip/ip_addr.h" +#include "wifi_hotspot.h" +#include "wifi_hotspot_config.h" +#include "iot_softap_xradio.h" +#include "iot_demo_def.h" + +#define DEFAULT_AP_NAME "TestIotAP" + +int BOARD_SoftApStart(const char *ap_name) +{ + HotspotConfig config = {0}; + char *apName = ap_name; + int retval; + + if (IsHotspotActive() == WIFI_HOTSPOT_ACTIVE) { + RaiseLog(LOG_LEVEL_ERR, "WIFI_HOTSPOT_ACTIVE \n"); + return -1; + } + if (apName == NULL) { + apName = DEFAULT_AP_NAME; + } + if (strcpy_s(config.ssid, sizeof(config.ssid), apName) != 0) { + RaiseLog(LOG_LEVEL_ERR, "[sample] strcpy ssid fail.\n"); + return -1; + } + + config.band = HOTSPOT_BAND_TYPE_2G; + config.securityType = WIFI_SEC_TYPE_OPEN; + retval = SetHotspotConfig((const HotspotConfig *)(&config)); + if (retval != WIFI_SUCCESS) { + RaiseLog(LOG_LEVEL_ERR, "SetHotspotConfig \n"); + return -1; + } + + retval = EnableHotspot(); + if (retval != WIFI_SUCCESS) { + RaiseLog(LOG_LEVEL_ERR, "EnableHotspot failed! \n"); + return -1; + } + + return 0; +} + +void BOARD_SoftApStop(void) +{ + if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) { + RaiseLog(LOG_LEVEL_ERR, "WIFI_HOTSPOT_NOT_ACTIVE \n"); + return; + } + + DisableHotspot(); +} diff --git a/common/iot_wifi_xradio/iot_wifi_xradio.c b/common/iot_wifi_xradio/iot_wifi_xradio.c new file mode 100644 index 0000000000000000000000000000000000000000..c8fcbf0db9c15c8ea6e1ae0264c61c4b0e50d65e --- /dev/null +++ b/common/iot_wifi_xradio/iot_wifi_xradio.c @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "lwip/netif.h" +#include "lwip/netifapi.h" +#include "lwip/ip4_addr.h" +#include "lwip/dhcp.h" +#include "cmsis_os2.h" +#include "wifi_device.h" +#include "wifi_error_code.h" +#include "ohos_init.h" +#include "iot_wifi_xradio.h" + +#define SLEEP_100MS 100 +#define SLEEP_200MS 200 + +#define DEF_TIMEOUT 15 +#define ONE_SECOND 1 +#define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK + +static void WiFiInit(void); +static void WaitSacnResult(void); +static int WaitConnectResult(void); +static void OnWifiScanStateChangedHandler(int state, int size); +static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info); +static void OnHotspotStaJoinHandler(StationInfo *info); +static void OnHotspotStateChangedHandler(int state); +static void OnHotspotStaLeaveHandler(StationInfo *info); + +static int g_staScanSuccess = 0; +static int g_ConnectSuccess = 0; +static int ssid_count = 0; +WifiEvent g_wifiEventHandler = {0}; +WifiErrorCode error; + +#define SELECT_WLAN_PORT "wlan0" + +int WifiConnect(const char *ssid, const char *psk) +{ + WifiScanInfo *info = NULL; + unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT; + + OS_MSleep(SLEEP_200MS); // wait 200ms for system init! + printf("<--System Init-->\r\n"); + + // 初始化WIFI + WiFiInit(); + + // 使能WIFI + if (EnableWifi() != WIFI_SUCCESS) { + printf("EnableWifi failed, error = %d\r\n", error); + return -1; + } + + // 判断WIFI是否激活 + if (IsWifiActive() == 0) { + printf("Wifi station is not actived.\r\n"); + return -1; + } + + // 分配空间,保存WiFi信息 + info = (WifiScanInfo *)malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT); + if (info == NULL) { + return -1; + } + // 轮询查找WiFi列表 + do { + // 重置标志位 + ssid_count = 0; + g_staScanSuccess = 0; + + // 开始扫描 + Scan(); + + // 等待扫描结果 + WaitSacnResult(); + + // 获取扫描列表 + error = GetScanInfoList(info, &size); + + } while (g_staScanSuccess != 1); + + for (uint8_t i = 0; i < ssid_count; i++) { + printf("no:%03d, ssid:%-30s, rssi:%5d\r\n", i+1, info[i].ssid, info[i].rssi/100); // reduce 100 + } + + // 连接指定的WiFi热点 + for (uint8_t i = 0; i < ssid_count; i++) { + if (strcmp(ssid, info[i].ssid) == 0) { + int result; + printf("Select:%3d wireless, Waiting...\r\n", i+1); + // 拷贝要连接的热点信息 + WifiDeviceConfig select_ap_config = {0}; + strcpy(select_ap_config.ssid, info[i].ssid); + strcpy(select_ap_config.preSharedKey, psk); + select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE; + + if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS) { + if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1) { + printf("WiFi connect succeed!\r\n"); + break; + } + } + } + + if (i == ssid_count - 1) { + printf("ERROR: No wifi as expected\r\n"); + while (1) OS_MSleep(SLEEP_100MS); // wait 100ms + } + } + + while (1) { + WifiLinkedInfo get_linked_res; + GetLinkedInfo(&get_linked_res); + if (strcmp(get_linked_res.ssid, ssid) == 0) { + break; + } + OS_MSleep(SLEEP_100MS); + } + + OS_MSleep(SLEEP_100MS); + + return 0; +} + +/**************************************************** +* 初始化WIFI +****************************************************/ +static void WiFiInit(void) +{ + printf("<--Wifi Init-->\r\n"); + g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler; + g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler; + g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler; + g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler; + g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler; + error = RegisterWifiEvent(&g_wifiEventHandler); + if (error != WIFI_SUCCESS) { + printf("register wifi event fail!\r\n"); + } else { + printf("register wifi event succeed!\r\n"); + } +} + +static void OnWifiScanStateChangedHandler(int state, int size) +{ + if (size > 0) { + ssid_count = size; + g_staScanSuccess = 1; + } + printf("callback function for wifi scan:%d, %d\r\n", state, size); + return; +} + +static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info) +{ + if (info == NULL) { + printf("WifiConnectionChanged:info is null, stat is %d.\n", state); + } else { + if (state == WIFI_STATE_AVALIABLE) { + g_ConnectSuccess = 1; + } else { + g_ConnectSuccess = 0; + } + } +} + +static void OnHotspotStaJoinHandler(StationInfo *info) +{ + (void)info; + printf("STA join AP\n"); + return; +} + +static void OnHotspotStaLeaveHandler(StationInfo *info) +{ + (void)info; + printf("HotspotStaLeave:info is null.\n"); + return; +} + +static void OnHotspotStateChangedHandler(int state) +{ + printf("HotspotStateChanged:state is %d.\n", state); + return; +} + +static void WaitSacnResult(void) +{ + int scanTimeout = DEF_TIMEOUT; + while (scanTimeout > 0) { + sleep(ONE_SECOND); + scanTimeout--; + if (g_staScanSuccess == 1) { + printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout)); + break; + } + } + if (scanTimeout <= 0) { + printf("WaitSacnResult:timeout!\n"); + } +} + +static int WaitConnectResult(void) +{ + int ConnectTimeout = DEF_TIMEOUT; + while (ConnectTimeout > 0) { + sleep(ONE_SECOND); + ConnectTimeout--; + if (g_ConnectSuccess == 1) { + printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout)); + break; + } + } + if (ConnectTimeout <= 0) { + printf("WaitConnectResult:timeout!\n"); + return 0; + } + return 1; +} + +int BOARD_InitWifi(void) +{ + return 0; +} + +int BOARD_ConnectWifi(const char *wifiSSID, const char *wifiPWD) +{ + return WifiConnect(wifiSSID, wifiPWD); +} + +int BOARD_DisconnectWifi() +{ + Disconnect(); + return 0; +} diff --git a/common/oled_api/oled_bmp.h b/common/oled_api/oled_bmp.h new file mode 100644 index 0000000000000000000000000000000000000000..7a8e25e076479a360dcdef5f07de444a3e633158 --- /dev/null +++ b/common/oled_api/oled_bmp.h @@ -0,0 +1,385 @@ +#ifndef __BMP_H__ +#define __BMP_H__ + +unsigned char BMP2[] = +{ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x7F,0xE0,0x03,0x91,0x1C,0x38,0x18,0x00,0x70,0x00,0xFF,0xFC,0x00,0x07,0xFF,0xFE, + 0x66,0x60,0x04,0x5B,0x22,0x44,0x14,0x01,0x8C,0x01,0x7F,0xFA,0x00,0x04,0x00,0x02, + 0x36,0xC0,0x08,0x15,0x40,0x80,0x52,0x02,0x02,0x01,0xBF,0xF6,0x00,0x3D,0xB6,0xDA, + 0x1F,0x80,0x88,0x11,0x40,0x80,0x34,0x04,0x71,0x01,0xDF,0xEE,0x00,0x25,0xB6,0xDA, + 0x0F,0x02,0x88,0x11,0x40,0x80,0x18,0x00,0x88,0x01,0xE0,0x1E,0x00,0x25,0xB6,0xDA, + 0x06,0x0A,0x88,0x11,0x40,0x80,0x34,0x01,0x04,0x01,0xDF,0xEE,0x00,0x25,0xB6,0xDA, + 0x06,0x2A,0x88,0x11,0x40,0x80,0x52,0x00,0x20,0x01,0xBF,0xF6,0x00,0x3D,0xB6,0xDA, + 0x06,0xAA,0x84,0x51,0x22,0x44,0x14,0x00,0x70,0x01,0x7F,0xFA,0x00,0x04,0x00,0x02, + 0x06,0xAA,0x83,0x91,0x1C,0x38,0x18,0x00,0x20,0x00,0xFF,0xFC,0x00,0x07,0xFF,0xFE, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x1F,0xFC,0x00,0x3F,0xFC,0x7F,0xF0,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x07,0x3F,0xFE,0x00,0x7F,0xFC,0xFF,0xF8,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x0F,0x30,0x06,0x00,0x60,0x00,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x1B,0x30,0x06,0x00,0x60,0x00,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x33,0x30,0x06,0x00,0x60,0x00,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x63,0x30,0x06,0x18,0x60,0x00,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0xC3,0x30,0x06,0x18,0x7F,0xFC,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x83,0x30,0x06,0x00,0x7F,0xFC,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x3F,0xFE,0x00,0x00,0x0C,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x3F,0xFE,0x00,0x00,0x0C,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x30,0x06,0x00,0x00,0x0C,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x30,0x06,0x18,0x00,0x0C,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x30,0x06,0x18,0x00,0x0C,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x30,0x06,0x00,0x00,0x0C,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x30,0x06,0x00,0x60,0x0C,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x30,0x06,0x00,0x60,0x0C,0xC0,0x18,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x3F,0xFE,0x00,0x7F,0xFC,0xFF,0xF8,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x1F,0xFC,0x00,0x3F,0xF8,0x7F,0xF0,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0xF9,0xE3,0x0F,0x00,0xF1,0xE0,0x1E,0x3C,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x01,0x0A,0x15,0x10,0x81,0x0A,0x10,0x21,0x42,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x12,0x11,0x10,0x81,0x0A,0x00,0x21,0x40,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x22,0x11,0x1F,0x81,0x0B,0xE0,0x21,0x7C,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x42,0x11,0x10,0x81,0x0A,0x10,0x21,0x42,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x82,0x11,0x10,0x99,0x0A,0x13,0x21,0x42,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x01,0xF9,0xE7,0xCF,0x18,0xF1,0xE3,0x1E,0x3C,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x41,0x1C,0x41,0x41,0x00,0x00,0x00,0x00,0x81,0x00,0x00,0x00,0x00,0x82,0x7C,0x82, + 0x63,0x22,0x61,0x41,0x00,0x00,0x00,0x00,0xF1,0x00,0x00,0x00,0x00,0xC6,0x82,0xC2, + 0x55,0x41,0x51,0x41,0x00,0x00,0x00,0x00,0x81,0x00,0x00,0x00,0x00,0xAA,0x80,0xA2, + 0x49,0x7E,0x49,0x41,0x00,0x00,0x00,0x00,0xFD,0x00,0x00,0x00,0x00,0x92,0x7C,0x92, + 0x41,0x40,0x45,0x41,0x00,0x00,0x00,0x00,0x81,0x00,0x00,0x00,0x00,0x82,0x02,0x8A, + 0x41,0x21,0x43,0x22,0x00,0x00,0x00,0x00,0xF1,0x00,0x00,0x00,0x00,0x82,0x82,0x86, + 0x41,0x1E,0x41,0x1C,0x00,0x00,0x00,0x00,0x81,0x00,0x00,0x00,0x00,0x82,0x7C,0x82, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +}; + +unsigned char BMP3[] = +{ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0xF8,0x03,0xFF,0xF8,0x03,0xC0,0xF0,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x0F,0xFE,0x03,0xFF,0xF8,0x07,0xF3,0xFC,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x18,0x03,0x03,0x00,0x00,0x0F,0xFF,0xFE,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x30,0x01,0x83,0x00,0x00,0x1F,0xFF,0xFF,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC3,0x00,0x00,0x1F,0xFF,0xFF,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC3,0x00,0x00,0x1F,0xFF,0xFF,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC3,0x00,0x00,0x0F,0xFF,0xFE,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC3,0x00,0x00,0x0F,0xFF,0xFE,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x30,0x01,0x83,0x7F,0xC0,0x07,0xFF,0xFC,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x30,0x03,0x03,0xFF,0xF0,0x03,0xFF,0xF8,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x18,0x06,0x03,0x80,0x38,0x01,0xFF,0xF0,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x0F,0xFC,0x03,0x00,0x18,0x00,0x7F,0xC0,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x1F,0xFF,0x00,0x00,0x0C,0x00,0x3F,0x80,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x30,0x01,0x80,0x00,0x0C,0x00,0x0E,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC0,0x00,0x0C,0x00,0x04,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC0,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC0,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC0,0x00,0x0C,0x10,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC0,0x00,0x0C,0x10,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC6,0x00,0x0C,0x1E,0x38,0xCC,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x60,0x00,0xC7,0x00,0x1C,0x11,0x45,0x32,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x30,0x01,0x83,0x80,0x38,0x11,0x45,0x22,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x1F,0xFF,0x01,0xFF,0xF0,0x11,0x45,0x22,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x0F,0xFE,0x00,0x7F,0xC0,0x11,0x45,0x22,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x79,0x22,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x22,0x00,0x90,0x04,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x42,0x00,0x90,0x04,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x06,0x40,0x41,0x01,0x08,0x08,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x04,0x20,0x81,0x01,0x08,0x08,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x08,0x20,0x81,0x02,0x04,0x10,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x08,0x20,0x80,0x82,0x04,0x10,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x10,0x21,0x00,0x82,0x02,0x10,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x20,0x11,0x00,0x84,0x02,0x10,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x40,0x12,0x00,0x44,0x02,0x20,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x80,0x12,0x00,0x48,0x01,0x20,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x48,0x01,0x20,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x30,0x01,0x20,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x30,0x00,0xC0,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +}; + +unsigned char BMP4[] = +{ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x80,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x01,0xE0,0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x01,0x00,0x0C,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x01,0x00,0x0F,0xC0,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x00,0x1F,0xFE,0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x00,0x3F,0xFF,0xFF,0x00,0x00,0x10,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x07,0x00,0x7F,0xFF,0xFF,0xFF,0xC0,0x10,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x06,0x00,0xFF,0xFF,0xFF,0xFF,0xE0,0x10,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x06,0x03,0xFF,0xFF,0xFF,0xFF,0xE0,0x20,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x06,0x0F,0xFF,0xFF,0xFF,0xFF,0xE0,0x60,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x06,0x3F,0xFF,0xFF,0xFF,0xFF,0xF0,0xC0,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x07,0x3C,0x3F,0xFF,0xF8,0x7F,0xF1,0x80,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x07,0x3F,0xFF,0xFF,0xFF,0xFF,0xF1,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x3F,0xFF,0xFF,0xFF,0xFF,0xF3,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x03,0x20,0x00,0xFF,0x00,0x07,0xF2,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x01,0x0F,0xFF,0x7E,0xFF,0xFB,0xE7,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x01,0x9F,0x7F,0x81,0xFD,0xFC,0x0F,0x80,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x01,0xBE,0x3F,0x9D,0xF8,0xFD,0xFF,0x80,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0xDF,0x7F,0xBC,0xFD,0xFB,0xFF,0x80,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x67,0xFE,0x7E,0x7F,0xE3,0xFF,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x38,0x01,0xFF,0x80,0x0F,0xFE,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x07,0xFE,0x00,0x7F,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xC7,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x0F,0x80,0x00,0x00,0x00,0x01,0x83,0x80,0x61,0xC0,0x00,0x00,0x00, + 0x00,0x00,0x00,0x0F,0x80,0xF0,0x00,0x00,0x07,0x82,0xE3,0xE1,0xB0,0x00,0x00,0x00, + 0x00,0x00,0x00,0x0F,0x80,0x1C,0x00,0x00,0x0C,0x6C,0x02,0x3F,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x0E,0x00,0x03,0x00,0x00,0x00,0xFE,0x00,0x3F,0x80,0x00,0x00,0x00, + 0x00,0x00,0x00,0x06,0x00,0xF1,0x80,0x00,0x0F,0x81,0xF7,0xC0,0x7C,0x00,0x00,0x00, + 0x00,0x00,0x00,0x03,0x80,0x18,0x80,0x00,0x03,0xFF,0x80,0xFF,0xE0,0x00,0x00,0x00, + 0x00,0x00,0x00,0x01,0xC0,0x0C,0x40,0x00,0x02,0x18,0x81,0x8C,0x60,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0xE1,0x84,0x40,0x00,0x02,0x18,0x81,0x8C,0x60,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x7F,0xC2,0x40,0x00,0x03,0xFF,0x81,0xFF,0xE0,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x3F,0xE2,0x40,0x00,0x02,0x00,0x31,0x80,0x0C,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x0F,0xC0,0x00,0x00,0x03,0xFF,0xF0,0xFF,0xFC,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xC0,0x7F,0xF0,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +}; + +////////////////////////////////////////////////////////////////////////////////// +// +//洢ͼƬݣͼƬСΪ51*32 +//ʽ˳򣨸λǰ +// +///////////////////////////////////////////////////////////////////////////////// +unsigned char BMP5[] = +{ + 0x00,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00, + 0x0E,0x00,0x00,0x00,0x00,0x01,0x80,0x0E,0x00,0x20,0x00,0x00,0x01,0xC0,0x00,0x00, + 0x70,0x00,0x00,0x00,0xC0,0x00,0x00,0xE0,0x00,0x00,0x00,0xE0,0x3F,0x81,0xC0,0x00, + 0x00,0x00,0x61,0xFF,0xF0,0x80,0x00,0x00,0x00,0x03,0xC0,0xF8,0x00,0x00,0x00,0x00, + 0x07,0x00,0x1C,0x00,0x00,0x00,0x00,0x1E,0x00,0x0F,0x00,0x00,0x00,0x7C,0x1C,0x00, + 0x03,0x1F,0x80,0x00,0x7C,0x1C,0x07,0xFF,0x1F,0x80,0x00,0x7C,0x1C,0x1F,0xFF,0x9F, + 0x80,0x00,0x00,0x1C,0x7C,0x03,0xF8,0x00,0x00,0x00,0x0F,0xF0,0x00,0x7C,0x00,0x00, + 0x03,0xFF,0x00,0x00,0x0F,0xE0,0x00,0x0F,0xF2,0x00,0x00,0x07,0xFE,0x00,0x1F,0x80, + 0x00,0x00,0x00,0x7F,0x80,0x3C,0x00,0x00,0x00,0x00,0x03,0x80,0x7C,0x00,0x00,0x00, + 0x00,0x03,0xC0,0x70,0x00,0x00,0x00,0x00,0x01,0xC0,0x70,0x00,0x00,0x00,0x00,0x01, + 0xC0,0x78,0x00,0x00,0x00,0x00,0x01,0xC0,0x7C,0x00,0x00,0x00,0x00,0x03,0xC0,0x3C, + 0x00,0x00,0x00,0x00,0x07,0x80,0x3E,0x00,0x00,0x00,0x00,0x3F,0x80,0x0F,0xE1,0xC0, + 0x00,0x1F,0xFC,0x00,0x03,0xFF,0xFC,0x03,0xFF,0xF0,0x00,0x00,0xFF,0xFF,0xFF,0xFF, + 0x00,0x00,0x00,0x00,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00, +}; +const unsigned char gImage_isoftstone[608] = { /* 0X00,0X01,0X80,0X00,0X26,0X00, */ +0X03,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X1F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X3F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X7F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X7F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XFF,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XFF,0XC0,0XC0,0X00,0X00,0X00,0X00,0X00,0X06,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XF0,0X07,0XF8,0X00,0X00,0X00,0X00,0X00,0X3F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00, +0XC0,0X0F,0XFE,0X00,0X00,0X00,0X00,0X00,0X7F,0XF0,0X00,0X00,0X00,0X00,0X00,0X00, +0X80,0X1F,0XFE,0X00,0X00,0X00,0X00,0X00,0XFF,0XF0,0X00,0X00,0X00,0X00,0X00,0X00, +0X0F,0X1E,0X0E,0X00,0X00,0X00,0X00,0X00,0XF0,0X70,0X00,0X00,0X00,0X00,0X00,0X00, +0X3F,0X3C,0X02,0X00,0X00,0X00,0X00,0X00,0XE0,0X10,0X00,0X00,0X00,0X00,0X00,0X00, +0X7F,0X1E,0X00,0X7F,0XF0,0X7F,0XE7,0XFC,0XF0,0X07,0XFE,0XFF,0XE0,0XF1,0XE3,0XFF, +0XFF,0X1F,0X00,0X7F,0XFC,0X7F,0XE7,0XFC,0XF8,0X07,0XFE,0XFF,0XF0,0XF1,0XE3,0XFF, +0XFF,0X1F,0XE0,0X7F,0XFC,0X7F,0XE7,0XFC,0XFF,0X07,0XFE,0XFF,0XF8,0XF9,0XE3,0XFF, +0XFF,0X0F,0XF8,0X7E,0X3E,0X78,0X00,0XE0,0X7F,0XC0,0XF0,0XF8,0X7C,0XF9,0XE3,0X80, +0XFF,0X03,0XFC,0X7C,0X1E,0X78,0X00,0XE0,0X1F,0XE0,0XF0,0XF0,0X3C,0XFD,0XE3,0X80, +0XFF,0X00,0X7E,0X78,0X0E,0X78,0X00,0XE0,0X03,0XF0,0XF0,0XF0,0X1C,0XFD,0XE3,0XFF, +0XFF,0X00,0X1F,0X78,0X0E,0X7F,0XE0,0XE0,0X00,0XF8,0XF0,0XF0,0X1C,0XFF,0XE3,0XFF, +0XFF,0X00,0X0F,0X78,0X0E,0X7F,0XE0,0XE0,0X00,0X78,0XF0,0XF0,0X1C,0XEF,0XE3,0XFF, +0XFF,0X10,0X0F,0X38,0X0E,0X7F,0XE0,0XE0,0X80,0X78,0XF0,0XF0,0X3C,0XE7,0XE3,0X80, +0XFF,0X1C,0X0F,0X3C,0X1E,0X7F,0XE0,0XE0,0XE0,0X78,0XF0,0X78,0X3C,0XE7,0XE3,0X80, +0XFF,0X1F,0XFE,0X3E,0X7E,0X78,0X00,0XE0,0XFF,0XF0,0XF0,0X7C,0XF8,0XE3,0XE3,0X80, +0XFF,0X1F,0XFE,0X1F,0XFC,0X78,0X00,0XE0,0XFF,0XF0,0XF0,0X3F,0XF8,0XE3,0XE3,0XFF, +0XFE,0X0F,0XFC,0X0F,0XF8,0X78,0X00,0XE0,0X7F,0XE0,0XF0,0X1F,0XF0,0XE3,0XE3,0XFF, +0XFC,0X03,0XF0,0X03,0XE0,0X78,0X00,0XE0,0X1F,0X80,0XF0,0X0F,0XC0,0XE1,0XE3,0XFF, +0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X20,0X02,0X00,0X01,0X80,0X00,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X3B,0XC3,0X0C,0X3F,0XE3,0XFC,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X26,0X43,0XFC,0X01,0XE0,0X84,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X39,0X03,0XD4,0X3D,0X60,0X84,0X00,0X00,0X00,0X00,0X00, +0XFF,0XFF,0XFF,0XFF,0XFC,0X39,0X83,0XFC,0X11,0X61,0X84,0X3F,0XFF,0XFF,0XFF,0XFF, +0X00,0X00,0X00,0X00,0X00,0X1B,0X83,0XFC,0X15,0X61,0X84,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X3A,0X83,0XDC,0X15,0X61,0X04,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X1A,0XC3,0X00,0X35,0X61,0X04,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X16,0X42,0XFC,0X3D,0X43,0X3C,0X00,0X00,0X00,0X00,0X00, +}; + +const unsigned char gImage_isoftstone2[608] = { /* 0X00,0X01,0X80,0X00,0X26,0X00, */ +0X03,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X1F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X3F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X7F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X7F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XFF,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XFF,0XC0,0XC0,0X00,0X00,0X00,0X00,0X00,0X06,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XF0,0X07,0XF8,0X00,0X00,0X00,0X00,0X00,0X3F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00, +0XC0,0X0F,0XFE,0X00,0X00,0X00,0X00,0X00,0X7F,0XF0,0X00,0X00,0X00,0X00,0X00,0X00, +0X80,0X1F,0XFE,0X00,0X00,0X00,0X00,0X00,0XFF,0XF0,0X00,0X00,0X00,0X00,0X00,0X00, +0X0F,0X1E,0X0E,0X00,0X00,0X00,0X00,0X00,0XF0,0X70,0X00,0X00,0X00,0X00,0X00,0X00, +0X3F,0X3C,0X02,0X00,0X00,0X00,0X00,0X00,0XE0,0X10,0X00,0X00,0X00,0X00,0X00,0X00, +0X7F,0X1E,0X00,0X7F,0XF0,0X7F,0XE7,0XFC,0XF0,0X07,0XFE,0XFF,0XE0,0XF1,0XE3,0XFF, +0XFF,0X1F,0X00,0X7F,0XFC,0X7F,0XE7,0XFC,0XF8,0X07,0XFE,0XFF,0XF0,0XF1,0XE3,0XFF, +0XFF,0X1F,0XE0,0X7F,0XFC,0X7F,0XE7,0XFC,0XFF,0X07,0XFE,0XFF,0XF8,0XF9,0XE3,0XFF, +0XFF,0X0F,0XF8,0X7E,0X3E,0X78,0X00,0XE0,0X7F,0XC0,0XF0,0XF8,0X7C,0XF9,0XE3,0X80, +0XFF,0X03,0XFC,0X7C,0X1E,0X78,0X00,0XE0,0X1F,0XE0,0XF0,0XF0,0X3C,0XFD,0XE3,0X80, +0XFF,0X00,0X7E,0X78,0X0E,0X78,0X00,0XE0,0X03,0XF0,0XF0,0XF0,0X1C,0XFD,0XE3,0XFF, +0XFF,0X00,0X1F,0X78,0X0E,0X7F,0XE0,0XE0,0X00,0XF8,0XF0,0XF0,0X1C,0XFF,0XE3,0XFF, +0XFF,0X00,0X0F,0X78,0X0E,0X7F,0XE0,0XE0,0X00,0X78,0XF0,0XF0,0X1C,0XEF,0XE3,0XFF, +0XFF,0X10,0X0F,0X38,0X0E,0X7F,0XE0,0XE0,0X80,0X78,0XF0,0XF0,0X3C,0XE7,0XE3,0X80, +0XFF,0X1C,0X0F,0X3C,0X1E,0X7F,0XE0,0XE0,0XE0,0X78,0XF0,0X78,0X3C,0XE7,0XE3,0X80, +0XFF,0X1F,0XFE,0X3E,0X7E,0X78,0X00,0XE0,0XFF,0XF0,0XF0,0X7C,0XF8,0XE3,0XE3,0X80, +0XFF,0X1F,0XFE,0X1F,0XFC,0X78,0X00,0XE0,0XFF,0XF0,0XF0,0X3F,0XF8,0XE3,0XE3,0XFF, +0XFE,0X0F,0XFC,0X0F,0XF8,0X78,0X00,0XE0,0X7F,0XE0,0XF0,0X1F,0XF0,0XE3,0XE3,0XFF, +0XFC,0X03,0XF0,0X03,0XE0,0X78,0X00,0XE0,0X1F,0X80,0XF0,0X0F,0XC0,0XE1,0XE3,0XFF, +0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X20,0X02,0X00,0X01,0X80,0X00,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X3B,0XC3,0X0C,0X3F,0XE3,0XFC,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X26,0X43,0XFC,0X01,0XE0,0X84,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X39,0X03,0XD4,0X3D,0X60,0X84,0X00,0X00,0X00,0X00,0X00, +0XFF,0XFF,0XFF,0XFF,0XFC,0X39,0X83,0XFC,0X11,0X61,0X84,0X3F,0XFF,0XFF,0XFF,0XFF, +0X00,0X00,0X00,0X00,0X00,0X1B,0X83,0XFC,0X15,0X61,0X84,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X3A,0X83,0XDC,0X15,0X61,0X04,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X1A,0XC3,0X00,0X35,0X61,0X04,0X00,0X00,0X00,0X00,0X00, +0X00,0X00,0X00,0X00,0X00,0X16,0X42,0XFC,0X3D,0X43,0X3C,0X00,0X00,0X00,0X00,0X00, +}; + +const unsigned char gImage_isoftstone3[416] = { /* 0X00,0X01,0X80,0X00,0X1A,0X00, */ +0X03,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X1F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X3F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X7F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0X7F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XFF,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XFF,0XC0,0XC0,0X00,0X00,0X00,0X00,0X00,0X06,0X00,0X00,0X00,0X00,0X00,0X00,0X00, +0XF0,0X07,0XF8,0X00,0X00,0X00,0X00,0X00,0X3F,0XC0,0X00,0X00,0X00,0X00,0X00,0X00, +0XC0,0X0F,0XFE,0X00,0X00,0X00,0X00,0X00,0X7F,0XF0,0X00,0X00,0X00,0X00,0X00,0X00, +0X80,0X1F,0XFE,0X00,0X00,0X00,0X00,0X00,0XFF,0XF0,0X00,0X00,0X00,0X00,0X00,0X00, +0X0F,0X1E,0X0E,0X00,0X00,0X00,0X00,0X00,0XF0,0X70,0X00,0X00,0X00,0X00,0X00,0X00, +0X3F,0X3C,0X02,0X7F,0X80,0X7F,0XE7,0XFD,0XE0,0X17,0XFE,0XFF,0X00,0XE1,0XE3,0XFF, +0X7F,0X3E,0X00,0X7F,0XF0,0X7F,0XE7,0XFC,0XF0,0X07,0XFE,0XFF,0XE0,0XF1,0XE3,0XFF, +0XFF,0X1F,0X00,0X7F,0XFC,0X7F,0XE7,0XFC,0XF8,0X07,0XFE,0XFF,0XF0,0XF1,0XE3,0XFF, +0XFF,0X1F,0XE0,0X7F,0XFC,0X7F,0XE7,0XFC,0XFF,0X07,0XFE,0XFF,0XF8,0XF9,0XE3,0XFF, +0XFF,0X0F,0XF8,0X7E,0X3E,0X78,0X00,0XE0,0X3F,0XC0,0XF0,0XF8,0X7C,0XF9,0XE3,0X80, +0XFF,0X03,0XFC,0X7C,0X1E,0X78,0X00,0XE0,0X1F,0XF0,0XF0,0XF0,0X3C,0XFD,0XE3,0X80, +0XFF,0X00,0X7E,0X78,0X0E,0X78,0X00,0XE0,0X03,0XF0,0XF0,0XF0,0X3C,0XFD,0XE3,0XFF, +0XFF,0X00,0X1F,0X78,0X0E,0X7F,0XE0,0XE0,0X00,0XF8,0XF0,0XF0,0X1C,0XFF,0XE3,0XFF, +0XFF,0X10,0X0F,0X78,0X0E,0X7F,0XE0,0XE0,0X80,0X78,0XF0,0XF0,0X1C,0XEF,0XE3,0XFF, +0XFF,0X18,0X0F,0X38,0X0E,0X7F,0XE0,0XE0,0XC0,0X78,0XF0,0XF0,0X3C,0XE7,0XE3,0X80, +0XFF,0X1C,0X1F,0X3C,0X1E,0X7F,0XE0,0XE0,0XE0,0XF8,0XF0,0X78,0X3C,0XE7,0XE3,0X80, +0XFF,0X1F,0XFE,0X3F,0X7E,0X78,0X00,0XE0,0XFF,0XF0,0XF0,0X7E,0XF8,0XE3,0XE3,0X80, +0XFF,0X1F,0XFE,0X1F,0XFC,0X78,0X00,0XE0,0XFF,0XF0,0XF0,0X3F,0XF8,0XE3,0XE3,0XFF, +0XFE,0X0F,0XFC,0X0F,0XF8,0X78,0X00,0XE0,0X7F,0XE0,0XF0,0X1F,0XF0,0XE1,0XE3,0XFF, +0XF8,0X03,0XF0,0X03,0XE0,0X78,0X00,0XE0,0X1F,0X80,0XF0,0X07,0XC0,0XE1,0XE3,0XFF, +}; + +const unsigned char gImage_issedu[] = { + 0x00,0x18,0xFF,0x8C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x27,0xFF,0xF3,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0xDF,0x80,0xFC,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x01,0x3D,0xFF,0xCE,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x02,0xF7,0xFF,0xF3,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x05,0xDF,0xFF,0xFD,0xD0,0x01,0x83,0x80,0xF7,0xFF,0x80,0xC1,0xC0,0x00,0xC0,0x00, + 0x0B,0xBF,0xFF,0xFE,0xE8,0x0E,0x74,0x7D,0x00,0x00,0x4F,0x3E,0x3E,0xFF,0x3F,0xC0, + 0x17,0x7F,0xFF,0xFF,0xF4,0x11,0xC3,0x82,0xF7,0xFF,0xB0,0xC1,0xC0,0x00,0xC0,0x00, + 0x16,0xFF,0xFF,0xFF,0xFA,0x2F,0xFB,0xFC,0xFF,0xFF,0xCF,0xFD,0xFF,0xFF,0xFF,0xE0, + 0x2D,0xFF,0xC0,0xFF,0xDA,0x2F,0xFB,0xFE,0xFF,0xFF,0xCF,0xFD,0xFE,0xFF,0xFF,0xE0, + 0x5F,0xFE,0x04,0x3F,0xED,0x2F,0xFF,0xFE,0x0B,0x83,0xCF,0xFF,0xFE,0xFF,0xFF,0xD0, + 0x5B,0xFD,0xFB,0xCF,0xFD,0x13,0x8F,0x0E,0xE7,0xFF,0xD0,0xDF,0x9C,0xEF,0xFD,0xD0, + 0xBF,0xFB,0xFF,0xC7,0xF6,0x97,0xEE,0xFE,0xF7,0xFF,0xEF,0xFF,0x5C,0xFF,0xFF,0xD0, + 0xB7,0xF7,0xF3,0xD7,0xFE,0x97,0xED,0xDC,0xFF,0xBB,0xEF,0xFD,0xDD,0x7F,0xFF,0x90, + 0xB7,0xEF,0xEB,0xDB,0xF7,0x57,0xE1,0xF3,0x7F,0xFF,0xE7,0xF1,0xDD,0x7F,0xFF,0xA0, + 0x7B,0x8F,0xD9,0xD4,0xEF,0x57,0xF9,0xF5,0x7F,0xFF,0xEF,0xFD,0xDC,0xFF,0xFF,0xD0, + 0x7E,0xEF,0xC7,0xEB,0x9F,0x57,0xF9,0xF5,0x7F,0xBB,0xEF,0xFD,0xDC,0xFF,0xFF,0xD0, + 0x7F,0xEF,0x9F,0xF5,0xFF,0x4B,0xFD,0xF3,0x7F,0xFF,0xEC,0x3C,0xFA,0xFF,0xFF,0xD0, + 0x7A,0xDF,0xBD,0xF5,0xEF,0x50,0xE5,0xFB,0x7F,0xFF,0xEF,0xFE,0xFA,0xFF,0xFF,0xD0, + 0x7F,0xDF,0x63,0xF9,0xFF,0x6F,0xFB,0xBB,0x7F,0xBB,0xEF,0xFD,0xFA,0xFF,0xFF,0xD0, + 0x7F,0xDF,0x57,0xF9,0xFF,0x6F,0xFB,0xBD,0x7E,0xBB,0xCF,0xFC,0x72,0xFF,0xFF,0xD0, + 0x7F,0xDF,0x67,0xFD,0xFF,0x4F,0xF7,0x5E,0x76,0x3B,0x90,0x71,0xFC,0xFF,0xFF,0xD0, + 0x7B,0xDF,0x15,0xFD,0xFF,0x40,0xEF,0x6E,0xFF,0xFF,0xE7,0xFB,0xFE,0xE0,0x03,0xD0, + 0x7F,0xFF,0xFB,0xFB,0xFF,0x4E,0xEE,0xA6,0xFF,0xFF,0xE7,0xEB,0x8E,0xE8,0x0B,0xD0, + 0x3F,0xFF,0xF7,0xFB,0xEF,0x44,0xE9,0xF2,0xCF,0xFF,0xE7,0xD2,0x76,0xFF,0xFB,0x20, + 0xBF,0xFF,0xFF,0xC7,0xFE,0x42,0x12,0x01,0x20,0x00,0x08,0x0C,0xB1,0x00,0x90,0x40, + 0xBF,0xF7,0xFF,0xB7,0xDE,0x85,0xCC,0xFE,0xFD,0xD7,0xF7,0xF2,0x4E,0xBF,0x65,0xC0, + 0x5E,0xFB,0xFF,0x4F,0xFE,0x97,0xDF,0xFF,0xFF,0xFB,0xF7,0xFE,0xFE,0xFF,0xF6,0xD0, + 0x5A,0xFE,0xF0,0xBF,0xBD,0x17,0xB3,0xEF,0xDB,0x7F,0xF7,0x6E,0xF5,0xCB,0x9F,0xD0, + 0x2F,0x7F,0x80,0xFD,0xFD,0x17,0xF3,0xED,0xFB,0x3F,0xF7,0xEE,0x71,0xEB,0x9F,0xD0, + 0x2F,0xFF,0xFF,0xFF,0xDA,0x17,0xFF,0x8D,0x7B,0xFF,0xF7,0x7F,0xFF,0xEB,0xFD,0xD0, + 0x16,0xDF,0xFF,0xFF,0xB4,0x07,0xDF,0xAD,0xF9,0xFB,0xF7,0xFB,0xDF,0x3B,0xF6,0xD0, + 0x0B,0x77,0xFB,0xFB,0x64,0x00,0x20,0x52,0x04,0x04,0x08,0x04,0x20,0xC4,0x09,0x20, + 0x0D,0xBF,0xFF,0xEE,0xC8,0x07,0xDE,0x8D,0xE9,0xFB,0xF7,0xFB,0xDF,0x2B,0xF4,0xC0, + 0x06,0xFF,0x7B,0xFF,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x03,0x7F,0xFF,0xFF,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0xBF,0xFF,0xFE,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x4F,0xFF,0xF9,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x31,0xFF,0xE6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x0C,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +}; +#endif + diff --git a/common/oled_api/oled_font.h b/common/oled_api/oled_font.h new file mode 100644 index 0000000000000000000000000000000000000000..dc4b1fe4ee97d94d0037feb1a700155e004699cc --- /dev/null +++ b/common/oled_api/oled_font.h @@ -0,0 +1,510 @@ +#ifndef __OLED_FONT_H__ +#define __OLED_FONT_H__ + +//常用ASCII表 +//偏移量32 +//ASCII字符集 +//偏移量32 +//大小:6*8 +//逐行式,顺向(高位在前) +/************************************6*8的点阵************************************/ +const unsigned char F6x8[][8] = +{ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // sp + 0x10,0x10,0x10,0x10,0x00,0x10,0x00,0x00, // ! + 0x28,0x28,0x28,0x00,0x00,0x00,0x00,0x00, // " + 0x28,0x28,0x7C,0x28,0x7C,0x28,0x28,0x00, // # + 0x10,0x3C,0x50,0x38,0x14,0x78,0x10,0x00, // $ + 0x00,0x4C,0x2C,0x10,0x08,0x60,0x60,0x00, // % + 0x30,0x48,0x50,0x20,0x54,0x48,0x34,0x00, // & + 0x30,0x10,0x20,0x00,0x00,0x00,0x00,0x00, // ' + 0x08,0x10,0x20,0x20,0x20,0x10,0x08,0x00, // ( + 0x20,0x10,0x08,0x08,0x08,0x10,0x20,0x00, // ) + 0x00,0x10,0x54,0x38,0x54,0x10,0x00,0x00, // * + 0x00,0x10,0x10,0x7C,0x10,0x10,0x00,0x00, // + + 0x00,0x00,0x00,0x00,0x00,0x18,0x08,0x10, // , + 0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00, // - + 0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00, // . + 0x00,0x04,0x08,0x10,0x20,0x40,0x00,0x00, // / + 0x38,0x44,0x4C,0x54,0x64,0x44,0x38,0x00, // 0 + 0x10,0x30,0x10,0x10,0x10,0x10,0x38,0x00, // 1 + 0x38,0x44,0x04,0x08,0x10,0x20,0x7C,0x00, // 2 + 0x7C,0x08,0x10,0x08,0x04,0x44,0x38,0x00, // 3 + 0x08,0x18,0x28,0x48,0x7C,0x08,0x08,0x00, // 4 + 0x7C,0x40,0x78,0x04,0x04,0x44,0x38,0x00, // 5 + 0x18,0x20,0x40,0x78,0x44,0x44,0x38,0x00, // 6 + 0x7C,0x04,0x08,0x10,0x20,0x20,0x20,0x00, // 7 + 0x38,0x44,0x44,0x38,0x44,0x44,0x38,0x00, // 8 + 0x38,0x44,0x44,0x3C,0x04,0x08,0x30,0x00, // 9 + 0x00,0x30,0x30,0x00,0x30,0x30,0x00,0x00, // : + 0x00,0x30,0x30,0x00,0x30,0x10,0x20,0x00, // ; + 0x08,0x10,0x20,0x40,0x20,0x10,0x08,0x00, // < + 0x00,0x00,0x7C,0x00,0x7C,0x00,0x00,0x00, // = + 0x20,0x10,0x08,0x04,0x08,0x10,0x20,0x00, // > + 0x38,0x44,0x04,0x08,0x10,0x00,0x10,0x00, // ? + 0x38,0x44,0x04,0x34,0x5C,0x44,0x38,0x00, // @ + 0x10,0x28,0x44,0x44,0x7C,0x44,0x44,0x00, // A + 0x78,0x44,0x44,0x78,0x44,0x44,0x78,0x00, // B + 0x38,0x44,0x40,0x40,0x40,0x44,0x38,0x00, // C + 0x70,0x48,0x44,0x44,0x44,0x48,0x70,0x00, // D + 0x7C,0x40,0x40,0x78,0x40,0x40,0x7C,0x00, // E + 0x7C,0x40,0x40,0x78,0x40,0x40,0x40,0x00, // F + 0x38,0x44,0x40,0x5C,0x44,0x44,0x3C,0x00, // G + 0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x00, // H + 0x38,0x10,0x10,0x10,0x10,0x10,0x38,0x00, // I + 0x1C,0x08,0x08,0x08,0x08,0x48,0x30,0x00, // J + 0x44,0x48,0x50,0x60,0x50,0x48,0x44,0x00, // K + 0x40,0x40,0x40,0x40,0x40,0x40,0x7C,0x00, // L + 0x44,0x6C,0x54,0x54,0x44,0x44,0x44,0x00, // M + 0x44,0x44,0x64,0x54,0x4C,0x44,0x44,0x00, // N + 0x38,0x44,0x44,0x44,0x44,0x44,0x38,0x00, // O + 0x78,0x44,0x44,0x78,0x40,0x40,0x40,0x00, // P + 0x38,0x44,0x44,0x44,0x54,0x48,0x34,0x00, // Q + 0x78,0x44,0x44,0x78,0x50,0x48,0x44,0x00, // R + 0x3C,0x40,0x40,0x38,0x04,0x04,0x78,0x00, // S + 0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x00, // T + 0x44,0x44,0x44,0x44,0x44,0x44,0x38,0x00, // U + 0x44,0x44,0x44,0x44,0x44,0x28,0x10,0x00, // V + 0x44,0x44,0x44,0x54,0x54,0x54,0x28,0x00, // W + 0x44,0x44,0x28,0x10,0x28,0x44,0x44,0x00, // X + 0x44,0x44,0x44,0x28,0x10,0x10,0x10,0x00, // Y + 0x7C,0x04,0x08,0x10,0x20,0x40,0x7C,0x00, // Z + 0x38,0x20,0x20,0x20,0x20,0x20,0x38,0x00, // [ + 0x00,0x40,0x20,0x10,0x08,0x04,0x00,0x00, // '\' + 0x38,0x08,0x08,0x08,0x08,0x08,0x38,0x00, // ] + 0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00, // ^ + 0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00, // _ + 0x20,0x10,0x08,0x00,0x00,0x00,0x00,0x00, // ' + 0x00,0x00,0x38,0x04,0x3C,0x44,0x3C,0x00, // a + 0x40,0x40,0x58,0x64,0x44,0x44,0x78,0x00, // b + 0x00,0x00,0x38,0x40,0x40,0x44,0x38,0x00, // c + 0x04,0x04,0x34,0x4C,0x44,0x44,0x3C,0x00, // d + 0x00,0x00,0x38,0x44,0x7C,0x40,0x38,0x00, // e + 0x38,0x24,0x20,0x70,0x20,0x20,0x20,0x00, // f + 0x00,0x00,0x3C,0x44,0x44,0x3C,0x04,0x38, // g + 0x40,0x40,0x58,0x64,0x44,0x44,0x44,0x00, // h + 0x10,0x00,0x30,0x10,0x10,0x10,0x38,0x00, // i + 0x08,0x00,0x18,0x08,0x08,0x08,0x48,0x30, // j + 0x40,0x40,0x48,0x50,0x60,0x50,0x48,0x00, // k + 0x30,0x10,0x10,0x10,0x10,0x10,0x38,0x00, // l + 0x00,0x00,0x68,0x54,0x54,0x44,0x44,0x00, // m + 0x00,0x00,0x58,0x64,0x44,0x44,0x44,0x00, // n + 0x00,0x00,0x38,0x44,0x44,0x44,0x38,0x00, // o + 0x00,0x00,0x58,0x64,0x64,0x58,0x40,0x40, // p + 0x00,0x00,0x34,0x4C,0x4C,0x34,0x04,0x04, // q + 0x00,0x00,0x58,0x64,0x40,0x40,0x40,0x00, // r + 0x00,0x00,0x3C,0x40,0x38,0x04,0x78,0x00, // s + 0x20,0x20,0x70,0x20,0x20,0x24,0x18,0x00, // t + 0x00,0x00,0x44,0x44,0x44,0x4C,0x34,0x00, // u + 0x00,0x00,0x44,0x44,0x44,0x28,0x10,0x00, // v + 0x00,0x00,0x44,0x44,0x54,0x54,0x28,0x00, // w + 0x00,0x00,0x44,0x28,0x10,0x28,0x44,0x00, // x + 0x00,0x00,0x44,0x44,0x44,0x3C,0x04,0x38, // y + 0x00,0x00,0x7C,0x08,0x10,0x20,0x7C,0x00, // z + 0x10,0x20,0x20,0x40,0x20,0x20,0x10,0x00, // { + 0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00, // | + 0x20,0x10,0x10,0x08,0x10,0x10,0x20,0x00, // } + 0x00,0x00,0x00,0x20,0x54,0x08,0x00,0x00, // ~ +}; + +//常用ASCII表 +//偏移量32 +//ASCII字符集 +//偏移量32 +//大小:8*16 +//逐行式,顺向(高位在前) +/****************************************8*16的点阵************************************/ +const unsigned char F8X16[]= +{ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0 + 0x00,0x00,0x00,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x18,0x18,0x00,0x00,//! 1 + 0x00,0x12,0x36,0x24,0x48,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2 + 0x00,0x00,0x00,0x24,0x24,0x24,0xFE,0x48,0x48,0x48,0xFE,0x48,0x48,0x48,0x00,0x00,//# 3 + 0x00,0x00,0x10,0x38,0x54,0x54,0x50,0x30,0x1C,0x14,0x14,0x54,0x54,0x38,0x10,0x10,//$ 4 + 0x00,0x00,0x00,0x44,0xA4,0xA8,0xA8,0xA8,0x54,0x1A,0x2A,0x2A,0x2A,0x44,0x00,0x00,//% 5 + 0x00,0x00,0x00,0x30,0x48,0x48,0x48,0x50,0x6E,0xA4,0x94,0x88,0x89,0x76,0x00,0x00,//& 6 + 0x00,0x60,0x60,0x20,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7 + 0x00,0x02,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x08,0x08,0x04,0x02,0x00,//( 8 + 0x00,0x40,0x20,0x10,0x10,0x08,0x08,0x08,0x08,0x08,0x08,0x10,0x10,0x20,0x40,0x00,//) 9 + 0x00,0x00,0x00,0x00,0x10,0x10,0xD6,0x38,0x38,0xD6,0x10,0x10,0x00,0x00,0x00,0x00,//* 10 + 0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0xFE,0x10,0x10,0x10,0x10,0x00,0x00,0x00,//+ 11 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x20,0xC0,//, 12 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//- 13 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00,//. 14 + 0x00,0x00,0x01,0x02,0x02,0x04,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x40,0x40,0x00,/// 15 + 0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00,//0 16 + 0x00,0x00,0x00,0x10,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//1 17 + 0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x04,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00,//2 18 + 0x00,0x00,0x00,0x3C,0x42,0x42,0x04,0x18,0x04,0x02,0x02,0x42,0x44,0x38,0x00,0x00,//3 19 + 0x00,0x00,0x00,0x04,0x0C,0x14,0x24,0x24,0x44,0x44,0x7E,0x04,0x04,0x1E,0x00,0x00,//4 20 + 0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x58,0x64,0x02,0x02,0x42,0x44,0x38,0x00,0x00,//5 21 + 0x00,0x00,0x00,0x1C,0x24,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x24,0x18,0x00,0x00,//6 22 + 0x00,0x00,0x00,0x7E,0x42,0x44,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00,//7 23 + 0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00,//8 24 + 0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x26,0x1A,0x02,0x02,0x24,0x38,0x00,0x00,//9 25 + 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,//: 26 + 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x08,0x30,0x00,//; 27 + 0x00,0x00,0x00,0x02,0x04,0x08,0x10,0x20,0x40,0x20,0x10,0x08,0x04,0x02,0x00,0x00,//< 28 + 0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,//= 29 + 0x00,0x00,0x00,0x40,0x20,0x10,0x08,0x04,0x02,0x04,0x08,0x10,0x20,0x40,0x00,0x00,//> 30 + 0x00,0x00,0x00,0x3C,0x42,0x42,0x62,0x02,0x04,0x08,0x08,0x00,0x18,0x18,0x00,0x00,//? 31 + 0x00,0x00,0x00,0x38,0x44,0x5A,0xAA,0xAA,0xAA,0xAA,0xB4,0x42,0x44,0x38,0x00,0x00,//@ 32 + 0x00,0x00,0x00,0x10,0x10,0x18,0x28,0x28,0x24,0x3C,0x44,0x42,0x42,0xE7,0x00,0x00,//A 33 + 0x00,0x00,0x00,0xF8,0x44,0x44,0x44,0x78,0x44,0x42,0x42,0x42,0x44,0xF8,0x00,0x00,//B 34 + 0x00,0x00,0x00,0x3E,0x42,0x42,0x80,0x80,0x80,0x80,0x80,0x42,0x44,0x38,0x00,0x00,//C 35 + 0x00,0x00,0x00,0xF8,0x44,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x44,0xF8,0x00,0x00,//D 36 + 0x00,0x00,0x00,0xFE,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x42,0x42,0xFE,0x00,0x00,//E 37 + 0x00,0x00,0x00,0xFE,0x42,0x48,0x48,0x78,0x48,0x48,0x40,0x40,0x40,0xE0,0x00,0x00,//F 38 + 0x00,0x00,0x00,0x3C,0x44,0x44,0x80,0x80,0x80,0x8E,0x84,0x44,0x44,0x38,0x00,0x00,//G 39 + 0x00,0x00,0x00,0xE7,0x42,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//H 40 + 0x00,0x00,0x00,0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//I 41 + 0x00,0x00,0x00,0x3E,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x88,0xF0,//J 42 + 0x00,0x00,0x00,0xEE,0x44,0x48,0x50,0x70,0x50,0x48,0x48,0x44,0x44,0xEE,0x00,0x00,//K 43 + 0x00,0x00,0x00,0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x42,0xFE,0x00,0x00,//L 44 + 0x00,0x00,0x00,0xEE,0x6C,0x6C,0x6C,0x6C,0x54,0x54,0x54,0x54,0x54,0xD6,0x00,0x00,//M 45 + 0x00,0x00,0x00,0xC7,0x62,0x62,0x52,0x52,0x4A,0x4A,0x4A,0x46,0x46,0xE2,0x00,0x00,//N 46 + 0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0x44,0x38,0x00,0x00,//O 47 + 0x00,0x00,0x00,0xFC,0x42,0x42,0x42,0x42,0x7C,0x40,0x40,0x40,0x40,0xE0,0x00,0x00,//P 48 + 0x00,0x00,0x00,0x38,0x44,0x82,0x82,0x82,0x82,0x82,0xB2,0xCA,0x4C,0x38,0x06,0x00,//Q 49 + 0x00,0x00,0x00,0xF8,0x44,0x44,0x44,0x78,0x48,0x48,0x44,0x44,0x42,0xE3,0x00,0x00,//R 50 + 0x00,0x00,0x00,0x3E,0x42,0x42,0x40,0x20,0x18,0x04,0x02,0x42,0x42,0x7C,0x00,0x00,//S 51 + 0x00,0x00,0x00,0xFE,0x92,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x38,0x00,0x00,//T 52 + 0x00,0x00,0x00,0xE7,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0x00,0x00,//U 53 + 0x00,0x00,0x00,0xE7,0x42,0x42,0x44,0x24,0x24,0x28,0x28,0x18,0x10,0x10,0x00,0x00,//V 54 + 0x00,0x00,0x00,0xD6,0x92,0x92,0x92,0x92,0xAA,0xAA,0x6C,0x44,0x44,0x44,0x00,0x00,//W 55 + 0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x18,0x18,0x18,0x24,0x24,0x42,0xE7,0x00,0x00,//X 56 + 0x00,0x00,0x00,0xEE,0x44,0x44,0x28,0x28,0x10,0x10,0x10,0x10,0x10,0x38,0x00,0x00,//Y 57 + 0x00,0x00,0x00,0x7E,0x84,0x04,0x08,0x08,0x10,0x20,0x20,0x42,0x42,0xFC,0x00,0x00,//Z 58 + 0x00,0x1E,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1E,0x00,//[ 59 + 0x00,0x00,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x04,0x04,0x02,0x02,0x02,0x01,0x01,//\ 60 + 0x00,0x78,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x78,0x00,//] 61 + 0x00,0x08,0x14,0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,//_ 63 + 0x00,0x60,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x1E,0x22,0x42,0x42,0x3F,0x00,0x00,//a 65 + 0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x64,0x58,0x00,0x00,//b 66 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x22,0x40,0x40,0x40,0x22,0x1C,0x00,0x00,//c 67 + 0x00,0x00,0x00,0x06,0x02,0x02,0x02,0x1E,0x22,0x42,0x42,0x42,0x26,0x1B,0x00,0x00,//d 68 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x7E,0x40,0x40,0x42,0x3C,0x00,0x00,//e 69 + 0x00,0x00,0x00,0x0F,0x11,0x10,0x10,0x7E,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//f 70 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x44,0x44,0x38,0x40,0x3C,0x42,0x42,0x3C,//g 71 + 0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x5C,0x62,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//h 72 + 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//i 73 + 0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,0x1C,0x04,0x04,0x04,0x04,0x04,0x04,0x44,0x78,//j 74 + 0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x4E,0x48,0x50,0x68,0x48,0x44,0xEE,0x00,0x00,//k 75 + 0x00,0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,//l 76 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF6,0x49,0x49,0x49,0x49,0x49,0xED,0x00,0x00,//m 77 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xDC,0x62,0x42,0x42,0x42,0x42,0xE7,0x00,0x00,//n 78 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x42,0x42,0x3C,0x00,0x00,//o 79 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD8,0x64,0x42,0x42,0x42,0x64,0x58,0x40,0xE0,//p 80 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x22,0x42,0x42,0x42,0x26,0x1A,0x02,0x07,//q 81 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xEE,0x32,0x20,0x20,0x20,0x20,0xF8,0x00,0x00,//r 82 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x42,0x40,0x3C,0x02,0x42,0x7C,0x00,0x00,//s 83 + 0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x7C,0x10,0x10,0x10,0x10,0x10,0x0C,0x00,0x00,//t 84 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC6,0x42,0x42,0x42,0x42,0x46,0x3B,0x00,0x00,//u 85 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x28,0x10,0x10,0x00,0x00,//v 86 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD7,0x92,0x92,0xAA,0xAA,0x44,0x44,0x00,0x00,//w 87 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6E,0x24,0x18,0x18,0x18,0x24,0x76,0x00,0x00,//x 88 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE7,0x42,0x24,0x24,0x28,0x18,0x10,0x10,0xE0,//y 89 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x44,0x08,0x10,0x10,0x22,0x7E,0x00,0x00,//z 90 + 0x00,0x03,0x04,0x04,0x04,0x04,0x04,0x08,0x04,0x04,0x04,0x04,0x04,0x04,0x03,0x00,//{ 91 + 0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,//| 92 + 0x00,0x60,0x10,0x10,0x10,0x10,0x10,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x60,0x00,//} 93 + 0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x52,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94 +}; +#if 0 +typedef struct +{ + unsigned char Index[2]; + char Msk[16]; +}typFNT_GB8; + +//宋体 +//8*8大小 +//逐行式,顺向(高位在前) +const typFNT_GB8 cfont8[] = +{ +#if 1 + "鸿",0x00,0x10,0x80,0x20,0x40,0x7C,0x1F,0x44,0x84,0x64,0x44,0x54,0x44,0x44,0x04,0x4C, + 0x24,0x40,0x44,0x7E,0xC4,0x02,0x47,0x02,0x5C,0x7A,0x48,0x02,0x40,0x0A,0x00,0x04,/*"鸿",0*/ + "蒙",0x08,0x20,0xFF,0xFE,0x08,0x20,0x7F,0xFE,0x40,0x02,0x8F,0xE4,0x00,0x00,0x7F,0xFC, + 0x06,0x00,0x3B,0x08,0x04,0xB0,0x19,0xC0,0x62,0xA0,0x0C,0x98,0x72,0x86,0x01,0x00,/*"蒙",1*/ + + "九",0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x7F,0xE0,0x04,0x20,0x04,0x20,0x04,0x20, + 0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x22,0x10,0x22,0x20,0x22,0x40,0x1E,0x80,0x00,/*"九",0*/ + "队",0x00,0x40,0x7C,0x40,0x44,0x40,0x48,0x40,0x48,0x40,0x50,0x40,0x48,0x40,0x48,0x40, + 0x44,0xA0,0x44,0xA0,0x44,0xA0,0x69,0x10,0x51,0x10,0x42,0x08,0x44,0x04,0x48,0x02,/*"队",1*/ +#endif +}; +#endif + +typedef struct +{ + unsigned char Index[2]; + char Msk[32]; +}typFNT_GB16; + +typedef struct +{ + unsigned char Index[3]; + char Msk[32]; +}typFNT_GB16_UTF; + +//宋体 +//16*16大小 +//逐行式,顺向(高位在前) +const typFNT_GB16 cfont16[] = +{ +#if 0 + "全",0x01,0x00,0x01,0x00,0x02,0x80,0x04,0x40,0x08,0x20,0x10,0x10,0x2F,0xE8,0xC1,0x06, + 0x01,0x00,0x01,0x00,0x1F,0xF0,0x01,0x00,0x01,0x00,0x01,0x00,0x7F,0xFC,0x00,0x00,/*"全",0*/ + "动",0x00,0x40,0x00,0x40,0x7C,0x40,0x00,0x40,0x01,0xFC,0x00,0x44,0xFE,0x44,0x20,0x44, + 0x20,0x44,0x20,0x84,0x48,0x84,0x44,0x84,0xFD,0x04,0x45,0x04,0x02,0x28,0x04,0x10,/*"动",1*/ + "电",0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8, + 0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x0A,0x01,0x02,0x01,0x02,0x00,0xFE,/*"电",0*/ + "子",0x00,0x00,0x7F,0xF8,0x00,0x10,0x00,0x20,0x00,0x40,0x01,0x80,0x01,0x00,0xFF,0xFE, + 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,/*"子",1*/ + "技",0x10,0x20,0x10,0x20,0x10,0x20,0x13,0xFE,0xFC,0x20,0x10,0x20,0x10,0x20,0x15,0xFC, + 0x18,0x84,0x30,0x88,0xD0,0x48,0x10,0x50,0x10,0x20,0x10,0x50,0x51,0x88,0x26,0x06,/*"技",2*/ + "术",0x01,0x00,0x01,0x20,0x01,0x10,0x01,0x10,0x7F,0xFC,0x03,0x80,0x05,0x40,0x05,0x40, + 0x09,0x20,0x11,0x10,0x21,0x08,0x41,0x04,0x81,0x02,0x01,0x00,0x01,0x00,0x01,0x00,/*"术",3*/ + "系",0x00,0xF8,0x3F,0x00,0x04,0x00,0x08,0x20,0x10,0x40,0x3F,0x80,0x01,0x00,0x06,0x10, + 0x18,0x08,0x7F,0xFC,0x01,0x04,0x09,0x20,0x11,0x10,0x21,0x08,0x45,0x04,0x02,0x00,/*"系",0*/ + "统",0x10,0x40,0x10,0x20,0x20,0x20,0x23,0xFE,0x48,0x40,0xF8,0x88,0x11,0x04,0x23,0xFE, + 0x40,0x92,0xF8,0x90,0x40,0x90,0x00,0x90,0x19,0x12,0xE1,0x12,0x42,0x0E,0x04,0x00,/*"统",1*/ + "设",0x00,0x00,0x21,0xF0,0x11,0x10,0x11,0x10,0x01,0x10,0x02,0x0E,0xF4,0x00,0x13,0xF8, + 0x11,0x08,0x11,0x10,0x10,0x90,0x14,0xA0,0x18,0x40,0x10,0xA0,0x03,0x18,0x0C,0x06,/*"设",2*/ + "置",0x7F,0xFC,0x44,0x44,0x7F,0xFC,0x01,0x00,0x7F,0xFC,0x01,0x00,0x1F,0xF0,0x10,0x10, + 0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0xFF,0xFE,0x00,0x00,/*"置",3*/ + "音",0x02,0x00,0x01,0x00,0x3F,0xF8,0x00,0x00,0x08,0x20,0x04,0x40,0xFF,0xFE,0x00,0x00, + 0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,/*"音",4*/ + "量",0x00,0x00,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0xFF,0xFE,0x00,0x00,0x1F,0xF0, + 0x11,0x10,0x1F,0xF0,0x11,0x10,0x1F,0xF0,0x01,0x00,0x1F,0xF0,0x01,0x00,0x7F,0xFC,/*"量",5*/ + "颜",0x10,0x00,0x08,0xFE,0x7F,0x10,0x22,0x20,0x14,0x7C,0x7F,0x44,0x44,0x54,0x48,0x54, + 0x52,0x54,0x44,0x54,0x48,0x54,0x51,0x54,0x42,0x28,0x44,0x24,0x88,0x42,0x30,0x82,/*"颜",6*/ + "色",0x08,0x00,0x08,0x00,0x1F,0xE0,0x20,0x20,0x40,0x40,0xBF,0xF8,0x21,0x08,0x21,0x08, + 0x21,0x08,0x3F,0xF8,0x20,0x00,0x20,0x02,0x20,0x02,0x20,0x02,0x1F,0xFE,0x00,0x00,/*"色",7*/ + "网",0x00,0x00,0x7F,0xFC,0x40,0x04,0x40,0x04,0x42,0x14,0x52,0x94,0x4A,0x54,0x44,0x24, + 0x44,0x24,0x4A,0x54,0x4A,0x54,0x52,0x94,0x61,0x04,0x40,0x04,0x40,0x14,0x40,0x08,/*"网",8*/ + "络",0x10,0x80,0x10,0x80,0x20,0xF8,0x21,0x08,0x4B,0x10,0xFC,0xA0,0x10,0x40,0x20,0xA0, + 0x41,0x18,0xFA,0x06,0x45,0xF8,0x01,0x08,0x19,0x08,0xE1,0x08,0x41,0xF8,0x01,0x08,/*"络",9*/ + +#endif +#if 1 + "鸿",0x00,0x10,0x80,0x20,0x40,0x7C,0x1F,0x44,0x84,0x64,0x44,0x54,0x44,0x44,0x04,0x4C, + 0x24,0x40,0x44,0x7E,0xC4,0x02,0x47,0x02,0x5C,0x7A,0x48,0x02,0x40,0x0A,0x00,0x04,/*"鸿",0*/ + "蒙",0x08,0x20,0xFF,0xFE,0x08,0x20,0x7F,0xFE,0x40,0x02,0x8F,0xE4,0x00,0x00,0x7F,0xFC, + 0x06,0x00,0x3B,0x08,0x04,0xB0,0x19,0xC0,0x62,0xA0,0x0C,0x98,0x72,0x86,0x01,0x00,/*"蒙",1*/ + + "九",0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x7F,0xE0,0x04,0x20,0x04,0x20,0x04,0x20, + 0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x22,0x10,0x22,0x20,0x22,0x40,0x1E,0x80,0x00,/*"九",0*/ + "队",0x00,0x40,0x7C,0x40,0x44,0x40,0x48,0x40,0x48,0x40,0x50,0x40,0x48,0x40,0x48,0x40, + 0x44,0xA0,0x44,0xA0,0x44,0xA0,0x69,0x10,0x51,0x10,0x42,0x08,0x44,0x04,0x48,0x02,/*"队",1*/ + + "软",0x00,0x00,0x18,0x60,0x10,0x40,0x7E,0x40,0x10,0xFE,0x20,0x84,0x29,0xB4,0x68,0x20, + 0x7F,0x20,0x08,0x30,0x08,0x30,0x7F,0x70,0x48,0x48,0x08,0x8C,0x0B,0x86,0x09,0x00,/*"软",0*/ + "通",0x00,0x00,0x27,0xFC,0x20,0x18,0x30,0xB0,0x00,0x60,0x07,0xFC,0x74,0x44,0x77,0xFC, + 0x34,0x44,0x34,0x44,0x37,0xFC,0x34,0x44,0x34,0x4C,0x38,0x00,0x47,0xFE,0x00,0x00,/*"通",1*/ + "动",0x00,0x00,0x00,0x30,0x00,0x30,0x3E,0x30,0x00,0x30,0x00,0xFE,0x7F,0x32,0x7F,0x22, + 0x10,0x22,0x16,0x22,0x22,0x22,0x23,0x62,0x7D,0x46,0x00,0xC6,0x01,0x9C,0x00,0x00,/*"动",2*/ + "力",0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xFE,0x01,0x04,0x01,0x04, + 0x03,0x04,0x03,0x04,0x02,0x04,0x06,0x04,0x0C,0x04,0x18,0x0C,0x70,0x38,0x20,0x00,/*"力",3*/ + + "鸿",0x00,0x10,0x80,0x20,0x40,0x7C,0x1F,0x44,0x84,0x64,0x44,0x54,0x44,0x44,0x04,0x4C, + 0x24,0x40,0x44,0x7E,0xC4,0x02,0x47,0x02,0x5C,0x7A,0x48,0x02,0x40,0x0A,0x00,0x04,/*"鸿",0*/ + "蒙",0x08,0x20,0xFF,0xFE,0x08,0x20,0x7F,0xFE,0x40,0x02,0x8F,0xE4,0x00,0x00,0x7F,0xFC, + 0x06,0x00,0x3B,0x08,0x04,0xB0,0x19,0xC0,0x62,0xA0,0x0C,0x98,0x72,0x86,0x01,0x00,/*"蒙",1*/ + "业",0x04,0x40,0x04,0x40,0x04,0x40,0x04,0x40,0x44,0x44,0x24,0x44,0x24,0x48,0x14,0x48, + 0x14,0x50,0x14,0x60,0x04,0x40,0x04,0x40,0x04,0x40,0x04,0x40,0xFF,0xFE,0x00,0x00,/*"业",2*/ + "务",0x04,0x00,0x04,0x00,0x0F,0xF0,0x18,0x20,0x24,0xC0,0x03,0x00,0x0C,0xC0,0x32,0x30, + 0xC2,0x0E,0x1F,0xF0,0x02,0x10,0x04,0x10,0x04,0x10,0x08,0x10,0x10,0xA0,0x20,0x40,/*"务",3*/ + "发",0x01,0x00,0x11,0x10,0x11,0x08,0x22,0x00,0x3F,0xFC,0x02,0x00,0x04,0x00,0x07,0xF8, + 0x0A,0x08,0x09,0x08,0x11,0x10,0x10,0xA0,0x20,0x40,0x40,0xA0,0x03,0x18,0x1C,0x06,/*"发",4*/ + "展",0x00,0x00,0x3F,0xFC,0x20,0x04,0x20,0x04,0x3F,0xFC,0x22,0x20,0x22,0x20,0x2F,0xF8, + 0x22,0x20,0x22,0x20,0x3F,0xFE,0x24,0x88,0x24,0x50,0x45,0x30,0x46,0x0E,0x84,0x00,/*"展",5*/ + "部",0x10,0x00,0x08,0x3E,0x7F,0xA2,0x00,0x24,0x21,0x24,0x12,0x28,0xFF,0xE4,0x00,0x24, + 0x00,0x22,0x3F,0x22,0x21,0x22,0x21,0x34,0x21,0x28,0x3F,0x20,0x21,0x20,0x00,0x20,/*"部",6*/ +#endif +}; + +const typFNT_GB16_UTF cfont16_UTF[] = +{ +#if 1 + "鸿",0x00,0x10,0x80,0x20,0x40,0x7C,0x1F,0x44,0x84,0x64,0x44,0x54,0x44,0x44,0x04,0x4C, + 0x24,0x40,0x44,0x7E,0xC4,0x02,0x47,0x02,0x5C,0x7A,0x48,0x02,0x40,0x0A,0x00,0x04,/*"鸿",0*/ + "蒙",0x08,0x20,0xFF,0xFE,0x08,0x20,0x7F,0xFE,0x40,0x02,0x8F,0xE4,0x00,0x00,0x7F,0xFC, + 0x06,0x00,0x3B,0x08,0x04,0xB0,0x19,0xC0,0x62,0xA0,0x0C,0x98,0x72,0x86,0x01,0x00,/*"蒙",1*/ + + "九",0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x7F,0xE0,0x04,0x20,0x04,0x20,0x04,0x20, + 0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x22,0x10,0x22,0x20,0x22,0x40,0x1E,0x80,0x00,/*"九",0*/ + "队",0x00,0x40,0x7C,0x40,0x44,0x40,0x48,0x40,0x48,0x40,0x50,0x40,0x48,0x40,0x48,0x40, + 0x44,0xA0,0x44,0xA0,0x44,0xA0,0x69,0x10,0x51,0x10,0x42,0x08,0x44,0x04,0x48,0x02,/*"队",1*/ + + "软",0x00,0x00,0x18,0x60,0x10,0x40,0x7E,0x40,0x10,0xFE,0x20,0x84,0x29,0xB4,0x68,0x20, + 0x7F,0x20,0x08,0x30,0x08,0x30,0x7F,0x70,0x48,0x48,0x08,0x8C,0x0B,0x86,0x09,0x00,/*"软",0*/ + "通",0x00,0x00,0x27,0xFC,0x20,0x18,0x30,0xB0,0x00,0x60,0x07,0xFC,0x74,0x44,0x77,0xFC, + 0x34,0x44,0x34,0x44,0x37,0xFC,0x34,0x44,0x34,0x4C,0x38,0x00,0x47,0xFE,0x00,0x00,/*"通",1*/ + "动",0x00,0x00,0x00,0x30,0x00,0x30,0x3E,0x30,0x00,0x30,0x00,0xFE,0x7F,0x32,0x7F,0x22, + 0x10,0x22,0x16,0x22,0x22,0x22,0x23,0x62,0x7D,0x46,0x00,0xC6,0x01,0x9C,0x00,0x00,/*"动",2*/ + "力",0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xFE,0x01,0x04,0x01,0x04, + 0x03,0x04,0x03,0x04,0x02,0x04,0x06,0x04,0x0C,0x04,0x18,0x0C,0x70,0x38,0x20,0x00,/*"力",3*/ + + "鸿",0x00,0x10,0x80,0x20,0x40,0x7C,0x1F,0x44,0x84,0x64,0x44,0x54,0x44,0x44,0x04,0x4C, + 0x24,0x40,0x44,0x7E,0xC4,0x02,0x47,0x02,0x5C,0x7A,0x48,0x02,0x40,0x0A,0x00,0x04,/*"鸿",0*/ + "蒙",0x08,0x20,0xFF,0xFE,0x08,0x20,0x7F,0xFE,0x40,0x02,0x8F,0xE4,0x00,0x00,0x7F,0xFC, + 0x06,0x00,0x3B,0x08,0x04,0xB0,0x19,0xC0,0x62,0xA0,0x0C,0x98,0x72,0x86,0x01,0x00,/*"蒙",1*/ + "业",0x04,0x40,0x04,0x40,0x04,0x40,0x04,0x40,0x44,0x44,0x24,0x44,0x24,0x48,0x14,0x48, + 0x14,0x50,0x14,0x60,0x04,0x40,0x04,0x40,0x04,0x40,0x04,0x40,0xFF,0xFE,0x00,0x00,/*"业",2*/ + "务",0x04,0x00,0x04,0x00,0x0F,0xF0,0x18,0x20,0x24,0xC0,0x03,0x00,0x0C,0xC0,0x32,0x30, + 0xC2,0x0E,0x1F,0xF0,0x02,0x10,0x04,0x10,0x04,0x10,0x08,0x10,0x10,0xA0,0x20,0x40,/*"务",3*/ + "发",0x01,0x00,0x11,0x10,0x11,0x08,0x22,0x00,0x3F,0xFC,0x02,0x00,0x04,0x00,0x07,0xF8, + 0x0A,0x08,0x09,0x08,0x11,0x10,0x10,0xA0,0x20,0x40,0x40,0xA0,0x03,0x18,0x1C,0x06,/*"发",4*/ + "展",0x00,0x00,0x3F,0xFC,0x20,0x04,0x20,0x04,0x3F,0xFC,0x22,0x20,0x22,0x20,0x2F,0xF8, + 0x22,0x20,0x22,0x20,0x3F,0xFE,0x24,0x88,0x24,0x50,0x45,0x30,0x46,0x0E,0x84,0x00,/*"展",5*/ + "部",0x10,0x00,0x08,0x3E,0x7F,0xA2,0x00,0x24,0x21,0x24,0x12,0x28,0xFF,0xE4,0x00,0x24, + 0x00,0x22,0x3F,0x22,0x21,0x22,0x21,0x34,0x21,0x28,0x3F,0x20,0x21,0x20,0x00,0x20,/*"部",6*/ +#endif +}; + + +typedef struct +{ + unsigned char Index[2]; + char Msk[72]; +}typFNT_GB24; + +typedef struct +{ + unsigned char Index[3]; + char Msk[72]; +}typFNT_GB24_UTF; + +//宋体 +//24*24大小 +//逐行式,顺向(高位在前) +const typFNT_GB24 cfont24[] = +{ + "全",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x30,0x00,0x00,0x68,0x00,0x00, + 0x44,0x00,0x00,0xC2,0x00,0x01,0x81,0x00,0x03,0x00,0xC0,0x06,0x00,0x70,0x0F,0xFF, + 0xFE,0x10,0x18,0x08,0x60,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x60, + 0x03,0xFF,0x80,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x08,0x1F, + 0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,/*"全",0*/ + "动",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x01,0x00,0x00,0x61,0x00,0x3F, + 0x81,0x00,0x00,0x01,0x00,0x00,0x01,0x0C,0x00,0x07,0xFC,0x00,0x31,0x0C,0x3F,0xC1, + 0x0C,0x06,0x01,0x0C,0x04,0x01,0x0C,0x0C,0x81,0x08,0x08,0x43,0x08,0x10,0x62,0x08, + 0x10,0x22,0x08,0x2F,0xF4,0x08,0x38,0x14,0x08,0x00,0x08,0x08,0x00,0x11,0x18,0x00, + 0x20,0xF8,0x00,0x40,0x30,0x00,0x00,0x00,/*"动",1*/ + "电",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x10,0x00,0x00,0x10,0x00,0x00, + 0x10,0x00,0x0F,0xFF,0xF0,0x08,0x10,0x20,0x08,0x10,0x20,0x08,0x10,0x20,0x08,0x10, + 0x20,0x0F,0xFF,0xE0,0x08,0x10,0x20,0x08,0x10,0x20,0x08,0x10,0x20,0x08,0x10,0x20, + 0x0F,0xFF,0xE0,0x08,0x10,0x00,0x00,0x10,0x08,0x00,0x10,0x08,0x00,0x10,0x04,0x00, + 0x18,0x0C,0x00,0x0F,0xF8,0x00,0x00,0x00,/*"电",2*/ + "子",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x0F,0xFF,0xF0,0x00,0x00,0x60,0x00, + 0x00,0x80,0x00,0x03,0x00,0x00,0x14,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18, + 0x0C,0x7F,0xFF,0xFE,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00, + 0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00, + 0xF8,0x00,0x00,0x30,0x00,0x00,0x00,0x00,/*"子",3*/ + "技",0x00,0x00,0x00,0x04,0x00,0x00,0x07,0x01,0x80,0x06,0x01,0x00,0x06,0x01,0x00,0x06, + 0x01,0x00,0x06,0xC1,0x0C,0x3F,0x3F,0xF0,0x06,0x01,0x00,0x06,0x01,0x00,0x06,0x01, + 0x00,0x06,0xFF,0xF8,0x07,0x08,0x10,0x3E,0x08,0x30,0x26,0x04,0x20,0x06,0x04,0x60, + 0x06,0x02,0x40,0x06,0x02,0xC0,0x06,0x01,0x80,0x06,0x03,0x80,0x06,0x0C,0x60,0x1E, + 0x10,0x3E,0x0C,0xE0,0x0C,0x00,0x00,0x00,/*"技",4*/ + "术",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x00,0x00,0x19,0x80,0x00,0x18,0xE0,0x00, + 0x18,0x60,0x00,0x18,0x28,0x00,0x18,0x1C,0x3F,0xFF,0xE0,0x00,0x3C,0x00,0x00,0x7C, + 0x00,0x00,0x5A,0x00,0x00,0xDA,0x00,0x00,0x99,0x00,0x01,0x19,0x80,0x03,0x18,0xC0, + 0x06,0x18,0x60,0x04,0x18,0x38,0x08,0x18,0x1E,0x30,0x18,0x08,0x40,0x18,0x00,0x00, + 0x18,0x00,0x00,0x18,0x00,0x00,0x00,0x00,/*"术",5*/ + + "鸿",0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x80,0x1C,0x01,0x80,0x0E,0x07,0xFC,0x04, + 0x06,0x0C,0x03,0xF6,0x4C,0x00,0xC6,0xCC,0x20,0xC6,0xCC,0x38,0xC6,0x48,0x1C,0xC6, + 0x08,0x00,0xC6,0x78,0x00,0xC6,0x20,0x00,0xC6,0x00,0x0C,0xC7,0xFE,0x0C,0xF0,0x06, + 0x1B,0xE0,0x06,0x1B,0x9F,0xE6,0x32,0x1F,0xE6,0x30,0x00,0x04,0x60,0x00,0x04,0x00, + 0x00,0x7C,0x00,0x00,0x3C,0x00,0x00,0x00,/*"鸿",0*/ + "蒙",0x00,0x00,0x00,0x00,0xC3,0x00,0x00,0xC3,0x00,0x3F,0xFF,0xFE,0x3F,0xFF,0xFE,0x00, + 0xC3,0x00,0x3F,0xFF,0xFC,0x3F,0xFF,0xFC,0x30,0x00,0x0C,0x37,0xFF,0xCC,0x00,0x00, + 0x00,0x00,0x00,0x00,0x1F,0xFF,0xFC,0x00,0x70,0x20,0x01,0xF8,0x60,0x1F,0x3D,0xE0, + 0x18,0x67,0x80,0x01,0xCE,0xC0,0x0F,0x1E,0x60,0x1C,0x72,0x70,0x01,0xE6,0x3E,0x0F, + 0x86,0x0C,0x3C,0x1E,0x00,0x00,0x10,0x00,/*"蒙",1*/ + + "九",0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x60,0x00,0x00,0x60,0x00,0x00,0x60,0x00,0x00, + 0xE0,0x00,0x00,0xE0,0x00,0x1F,0xFF,0x80,0x1F,0xFF,0x80,0x00,0xC1,0x80,0x00,0xC1, + 0x80,0x00,0xC1,0x80,0x00,0xC1,0x80,0x01,0xC1,0x80,0x01,0x81,0x80,0x01,0x81,0x80, + 0x03,0x01,0x80,0x03,0x01,0x84,0x06,0x01,0x86,0x0E,0x01,0x8E,0x1C,0x01,0x8C,0x38, + 0x01,0xFC,0x10,0x00,0xF8,0x00,0x00,0x00,/*"九",0*/ + "队",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x3F,0xC3,0x00,0x30,0xC3,0x00,0x30, + 0x83,0x00,0x31,0x83,0x00,0x31,0x83,0x00,0x31,0x03,0x00,0x33,0x03,0x00,0x33,0x03, + 0x00,0x31,0x83,0x80,0x31,0x87,0x80,0x30,0xC6,0x80,0x30,0xC6,0xC0,0x30,0xCC,0xC0, + 0x30,0xCC,0x60,0x37,0xD8,0x60,0x37,0x98,0x30,0x30,0x30,0x18,0x30,0x70,0x1C,0x30, + 0xE0,0x0E,0x30,0xC0,0x04,0x00,0x00,0x00,/*"队",1*/ + +}; + +typedef struct +{ + unsigned char Index[2]; + char Msk[128]; +}typFNT_GB32; + +typedef struct +{ + unsigned char Index[2]; + char Msk[128]; +}typFNT_GB32_UTF; + +//宋体 +//32*32大小 +//逐行式,顺向(高位在前) +const typFNT_GB32 cfont32[] = +{ + "全",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x03,0x80,0x00, + 0x00,0x07,0x80,0x00,0x00,0x06,0x80,0x00,0x00,0x0C,0x40,0x00,0x00,0x1C,0x20,0x00, + 0x00,0x18,0x30,0x00,0x00,0x30,0x18,0x00,0x00,0x60,0x0C,0x00,0x00,0xC0,0x07,0x00, + 0x01,0x80,0x03,0xC0,0x03,0x00,0x02,0xF8,0x07,0xFF,0xFF,0x7C,0x08,0x01,0x80,0x90, + 0x30,0x01,0x80,0x00,0x40,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x81,0x00, + 0x00,0x01,0x83,0x80,0x01,0xFF,0xFF,0xC0,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00, + 0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x10, + 0x00,0x01,0x80,0x38,0x1F,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"全",0*/ + "动",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x0E,0x00, + 0x00,0x00,0x0C,0x00,0x00,0x0C,0x0C,0x00,0x3F,0xFE,0x0C,0x00,0x10,0x00,0x0C,0x00, + 0x00,0x00,0x0C,0x00,0x00,0x00,0x0C,0x18,0x00,0x00,0xFF,0xFC,0x00,0x02,0x0C,0x18, + 0x00,0x07,0x0C,0x18,0x3F,0xFF,0x8C,0x18,0x01,0xC0,0x0C,0x18,0x01,0xC0,0x0C,0x18, + 0x01,0x80,0x08,0x18,0x03,0x00,0x08,0x18,0x02,0x10,0x18,0x18,0x06,0x08,0x18,0x18, + 0x04,0x0C,0x10,0x18,0x08,0x06,0x10,0x18,0x10,0x06,0x30,0x10,0x2F,0xFB,0x20,0x10, + 0x3E,0x03,0x60,0x10,0x10,0x02,0xC0,0x30,0x00,0x00,0x80,0x30,0x00,0x01,0x0C,0x30, + 0x00,0x02,0x03,0xE0,0x00,0x0C,0x01,0xE0,0x00,0x10,0x00,0x80,0x00,0x00,0x00,0x00,/*"动",1*/ + "电",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x03,0x80,0x00, + 0x00,0x03,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x03,0x00,0x00, + 0x07,0xFF,0xFF,0xC0,0x06,0x03,0x00,0xC0,0x06,0x03,0x00,0xC0,0x06,0x03,0x00,0xC0, + 0x06,0x03,0x00,0xC0,0x06,0x03,0x00,0xC0,0x06,0x03,0x00,0xC0,0x07,0xFF,0xFF,0xC0, + 0x06,0x03,0x00,0xC0,0x06,0x03,0x00,0xC0,0x06,0x03,0x00,0xC0,0x06,0x03,0x00,0xC0, + 0x06,0x03,0x00,0xC0,0x07,0xFF,0xFF,0xC0,0x06,0x03,0x00,0x80,0x06,0x03,0x00,0x00, + 0x00,0x03,0x00,0x10,0x00,0x03,0x00,0x08,0x00,0x03,0x00,0x08,0x00,0x03,0x00,0x18, + 0x00,0x03,0x80,0x1C,0x00,0x01,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"电",2*/ + "子",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0, + 0x03,0xFF,0xFF,0xE0,0x00,0x00,0x01,0xE0,0x00,0x00,0x03,0x00,0x00,0x00,0x06,0x00, + 0x00,0x00,0x18,0x00,0x00,0x00,0x30,0x00,0x00,0x01,0xC0,0x00,0x00,0x01,0xC0,0x00, + 0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x10,0x00,0x01,0x80,0x38,0x3F,0xFF,0xFF,0xFC, + 0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00, + 0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00, + 0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00, + 0x00,0x3F,0x80,0x00,0x00,0x07,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,/*"子",3*/ + "技",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x08,0x00,0x01,0xC0,0x0E,0x00, + 0x01,0x80,0x0C,0x00,0x01,0x80,0x0C,0x00,0x01,0x80,0x0C,0x00,0x01,0x80,0x0C,0x00, + 0x01,0x98,0x0C,0x18,0x3F,0xFF,0xFF,0xFC,0x01,0x80,0x0C,0x00,0x01,0x80,0x0C,0x00, + 0x01,0x80,0x0C,0x00,0x01,0x80,0x0C,0x00,0x01,0x8C,0x0C,0x20,0x01,0xB3,0xFF,0xF0, + 0x01,0xC0,0x80,0x60,0x0F,0x80,0x80,0x60,0x3D,0x80,0x40,0xC0,0x31,0x80,0x40,0xC0, + 0x01,0x80,0x21,0x80,0x01,0x80,0x21,0x80,0x01,0x80,0x33,0x00,0x01,0x80,0x1E,0x00, + 0x01,0x80,0x0C,0x00,0x01,0x80,0x1E,0x00,0x01,0x80,0x37,0x00,0x01,0x80,0xE3,0xC0, + 0x1F,0x83,0x80,0xF8,0x07,0x8E,0x00,0x7C,0x02,0x30,0x00,0x00,0x00,0x00,0x00,0x00,/*"技",4*/ + "术",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0xC0,0x00, + 0x00,0x01,0x8C,0x00,0x00,0x01,0x87,0x00,0x00,0x01,0x83,0x80,0x00,0x01,0x81,0x80, + 0x00,0x01,0x80,0x80,0x00,0x01,0x80,0x30,0x3F,0xFF,0xFF,0xF8,0x10,0x03,0xC0,0x00, + 0x00,0x07,0xC0,0x00,0x00,0x07,0xA0,0x00,0x00,0x0D,0xA0,0x00,0x00,0x0D,0x90,0x00, + 0x00,0x19,0x90,0x00,0x00,0x19,0x98,0x00,0x00,0x31,0x8C,0x00,0x00,0x61,0x86,0x00, + 0x00,0xC1,0x87,0x00,0x00,0x81,0x83,0x80,0x01,0x81,0x81,0xC0,0x03,0x01,0x80,0xF0, + 0x04,0x01,0x80,0x7C,0x08,0x01,0x80,0x38,0x10,0x01,0x80,0x10,0x60,0x01,0x80,0x00, + 0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,/*"术",5*/ +}; + +#endif + diff --git a/common/oled_api/oled_test.h b/common/oled_api/oled_test.h new file mode 100644 index 0000000000000000000000000000000000000000..c863a013dc6d0aea504f5ac3a66dcd08b0c6aac2 --- /dev/null +++ b/common/oled_api/oled_test.h @@ -0,0 +1,31 @@ +#ifndef __OLED_TEST_H__ +#define __OLED_TEST_H__ + +#include <_ansi.h> + +_BEGIN_STD_C + +//--------------OLED参数定义--------------------- +#define PAGE_SIZE 8 +#define XLevelL 0x02 //1.3寸XLevelL为0x02 0.9寸XLevelL为0x00 +#define XLevelH 0x10 +#define YLevel 0xB0 +#define Brightness 0xFF +#define WIDTH 128 +#define HEIGHT 64 + +void OledTestBorder(void); +void OledTestFonts(void); +void OledTestFPS(void); +void OledTestAll(void); +void OledTestLine(void); +void OledTestRectangle(void); +void OledTestCircle(void); +void OledTestArc(void); +void OledTestPolyline(void); +void OledMenu2(float temperature, float humidity); + + +_END_STD_C + +#endif // __OLED_TEST_H__ \ No newline at end of file diff --git a/common/oled_api/oled_tests.c b/common/oled_api/oled_tests.c new file mode 100644 index 0000000000000000000000000000000000000000..4cca6b9e586ee6354f4b2f6b05c32a086056e63c --- /dev/null +++ b/common/oled_api/oled_tests.c @@ -0,0 +1,311 @@ +#include +#include +#include "ssd1306_gui.h" +#include "oled_tests.h" +#include "oled_bmp.h" + +uint32_t HAL_GetTick(void) +{ + uint32_t msPerTick = 1000 / osKernelGetTickFreq(); // 10ms + uint32_t tickMs = osKernelGetTickCount() * msPerTick; + + uint32_t csPerMs = osKernelGetSysTimerFreq() / 1000; // 160K cycle/ms + uint32_t csPerTick = csPerMs * msPerTick; // 1600K cycles/tick + uint32_t restMs = osKernelGetSysTimerCount() % csPerTick / csPerMs; + + return tickMs + restMs; +} + +void OledTestBorder() { + SSD1306Clear(Black); + + uint32_t start = HAL_GetTick(); + uint32_t end = start; + uint8_t x = 0; + uint8_t y = 0; + do { + ssd1306_DrawPixel(x, y, Black); + ssd1306 + + if((y == 0) && (x < 127)) + x++; + else if((x == 127) && (y < 63)) + y++; + else if((y == 63) && (x > 0)) + x--; + else + y--; + + ssd1306_DrawPixel(x, y, White); + ssd1306_UpdateScreen(); + + end = HAL_GetTick(); + } while((end - start) < 8000); + + HAL_Delay(1000); +} + +void OledTestFonts() { + ssd1306_Fill(Black); + ssd1306_SetCursor(2, 0); + ssd1306_DrawString("Font 16x26", Font_16x26, White); + ssd1306_SetCursor(2, 26); + ssd1306_DrawString("Font 11x18", Font_11x18, White); + ssd1306_SetCursor(2, 26+18); + ssd1306_DrawString("Font 7x10", Font_7x10, White); + ssd1306_SetCursor(2, 26+18+10); + ssd1306_DrawString("Font 6x8", Font_6x8, White); + ssd1306_UpdateScreen(); +} + +void OledTestFPS() { + ssd1306_Fill(White); + + uint32_t start = HAL_GetTick(); + uint32_t end = start; + int fps = 0; + char message[] = "ABCDEFGHIJK"; + + ssd1306_SetCursor(2,0); + ssd1306_DrawString("Testing...", Font_11x18, Black); + + do { + ssd1306_SetCursor(2, 18); + ssd1306_DrawString(message, Font_11x18, Black); + ssd1306_UpdateScreen(); + + char ch = message[0]; + memmove(message, message+1, sizeof(message)-2); + message[sizeof(message)-2] = ch; + + fps++; + end = HAL_GetTick(); + } while((end - start) < 5000); + + HAL_Delay(1000); + + char buff[64]; + fps = (float)fps / ((end - start) / 1000.0); + snprintf(buff, sizeof(buff), "~%d FPS", fps); + + ssd1306_Fill(White); + ssd1306_SetCursor(2, 18); + ssd1306_DrawString(buff, Font_11x18, Black); + ssd1306_UpdateScreen(); +} + +void OledTestLine() { + + ssd1306_DrawLine(1,1,SSD1306_WIDTH - 1,SSD1306_HEIGHT - 1,White); + ssd1306_DrawLine(SSD1306_WIDTH - 1,1,1,SSD1306_HEIGHT - 1,White); + ssd1306_UpdateScreen(); + return; +} + +void OledTestRectangle() { + uint32_t delta; + + for(delta = 0; delta < 5; delta ++) { + ssd1306_DrawRectangle(1 + (5*delta),1 + (5*delta) ,SSD1306_WIDTH-1 - (5*delta),SSD1306_HEIGHT-1 - (5*delta),White); + } + ssd1306_UpdateScreen(); + return; +} + +void OledTestCircle() { + uint32_t delta; + + for(delta = 0; delta < 5; delta ++) { + ssd1306_DrawCircle(20* delta+30, 30, 10, White); + } + ssd1306_UpdateScreen(); + return; +} + +void OledTestArc() { + + ssd1306_DrawArc(30, 30, 30, 20, 270, White); + ssd1306_UpdateScreen(); + return; +} + +void OledTestPolyline() { + SSD1306_VERTEX loc_vertex[] = + { + {35,40}, + {40,20}, + {45,28}, + {50,10}, + {45,16}, + {50,10}, + {53,16} + }; + + ssd1306_DrawPolyline(loc_vertex,sizeof(loc_vertex)/sizeof(loc_vertex[0]),White); + ssd1306_UpdateScreen(); + return; +} + +static const unsigned char bitmap[] = { +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x0F, 0xF0, 0x00, 0x7F, 0xFF, 0xFF, 0xE0, 0x03, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x7F, 0xF8, 0x00, 0x3F, 0xFF, 0xFF, 0x80, 0x01, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFC, 0x00, 0x0F, 0xF8, 0x00, 0x0F, 0x80, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x7F, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE0, 0x7F, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x01, 0xF0, 0x3F, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x3F, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x1F, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x0F, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x07, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0x03, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0x87, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x07, 0xFE, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x3F, 0xFC, 0x03, 0xFC, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x7F, 0xFE, 0x07, 0xFC, 0x3E, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x7F, 0xF8, 0x0F, 0xFE, 0x7F, 0xC7, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x7F, 0xF0, 0xFF, 0xFE, 0x7F, 0xF3, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xF8, 0xFF, 0xFC, 0xFF, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x7F, 0xFC, 0xFF, 0x1B, 0xFF, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0x7E, 0x37, 0xFF, 0xE7, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F, 0xFF, 0x81, 0xEF, 0xFF, 0x8F, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xFF, 0xFF, 0xDF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFE, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +}; + +void OledTestBitmap(void) { + ssd1306_Fill(Black); + ssd1306_DrawBitmap(bitmap, sizeof(bitmap)); + ssd1306_UpdateScreen(); +} + +void OledTestAll() { + ssd1306_Init(); + ssd1306_TestBitmap(); + HAL_Delay(2000); + ssd1306_TestFPS(); + HAL_Delay(1000); + ssd1306_TestBorder(); + ssd1306_TestFonts(); + HAL_Delay(3000); + ssd1306_Fill(Black); + ssd1306_TestRectangle(); + ssd1306_TestLine(); + HAL_Delay(3000); + ssd1306_Fill(Black); + ssd1306_TestPolyline(); + HAL_Delay(3000); + ssd1306_Fill(Black); + ssd1306_TestArc(); + HAL_Delay(3000); + ssd1306_Fill(Black); + ssd1306_TestCircle(); + HAL_Delay(3000); +} + +void OledMenu2(float temperature, float humidity) +{ + unsigned char buf[64] = {0}; + + OLED_Clear(0); + + unsigned char i; + srand(123456); + ssd1306_DrawLine(0, 10, WIDTH-1, 10, White); + ssd1306_DrawLine(WIDTH/2-1,11,WIDTH/2-1,HEIGHT-1,1); + ssd1306_DrawLine(WIDTH/2-1,10+(HEIGHT-10)/2-1,WIDTH-1,10+(HEIGHT-10)/2-1,1); + ssd1306_SetCursor(0, 1); + ssd1306_DrawString("2018-08-25", Font_6x8, White); + ssd1306_SetCursor(78,1); + ssd1306_DrawString("Saturday", Font_6x8, White); + ssd1306_SetCursor(14,HEIGHT-1-10); + ssd1306_DrawString("Cloudy", Font_6x8, White); + ssd1306_SetCursor(WIDTH/2-1+2,13); + ssd1306_DrawString("TEMP", Font_6x8, White); + ssd1306_DrawCircle(WIDTH-1-19, 25, 2, 1); + ssd1306_SetCursor(WIDTH-1-14,20); + ssd1306_DrawString("C", Font_11x18, White); + /*memset(buf, 0x00, sizeof(buf)); + sprintf(buf, "%.1f", temperature); + ssd1306_SetCursor(WIDTH/2-1+9,20); + ssd1306_DrawString(buf, Font_11x18, White);*/ + ssd1306_DrawString("FONT", Font_11x18, White); + ssd1306_SetCursor(WIDTH/2-1+2,39); + ssd1306_DrawString("Humi", Font_6x8, White); + /*memset(buf, 0x00, sizeof(buf)); + sprintf(buf, "%.1f", humidity); + ssd1306_SetCursor(WIDTH/2-1+5,46); + ssd1306_DrawString(buf, Font_11x18, White);*/ + ssd1306_DrawString("FONT", Font_11x18, White); + ssd1306_DrawBitmap(BMP5, 51); + + OLED_Display(); + #if 0 + GUI_DrawLine(0, 10, WIDTH-1, 10,1); + GUI_DrawLine(WIDTH/2-1,11,WIDTH/2-1,HEIGHT-1,1); + GUI_DrawLine(WIDTH/2-1,10+(HEIGHT-10)/2-1,WIDTH-1,10+(HEIGHT-10)/2-1,1); + GUI_ShowString(0,1,"2018-08-25",8,1); + GUI_ShowString(78,1,"Saturday",8,1); + GUI_ShowString(14,HEIGHT-1-10,"Cloudy",8,1); + GUI_ShowString(WIDTH/2-1+2,13,"TEMP",8,1); + GUI_DrawCircle(WIDTH-1-19, 25, 1,2); + GUI_ShowString(WIDTH-1-14,20,"C",16,1); + memset(buf, 0x00, sizeof(buf)); + sprintf(buf, "%.1f", temperature); + GUI_ShowString(WIDTH/2-1+9,20,buf,16,1); + GUI_ShowString(WIDTH/2-1+2,39,"PM2.5",8,1); + GUI_ShowString(WIDTH/2-1+5,46,"90ug/m3",16,1); + GUI_DrawBMP(6,16,51,32, BMP5, 1); + for(i=0;i<15;i++) + { + GUI_ShowNum(WIDTH/2-1+9,20,rand()%4,1,16,1); + GUI_ShowNum(WIDTH/2-1+9+8,20,rand()%10,1,16,1); + GUI_ShowNum(WIDTH/2-1+9+8+16,20,rand()%10,1,16,1); + GUI_ShowNum(WIDTH/2-1+5,46,rand()%10,1,16,1); + GUI_ShowNum(WIDTH/2-1+5+8,46,rand()%10,1,16,1); + msleep(500); + } + #endif +} + + diff --git a/common/ssd1306/ssd1306_conf.h b/common/ssd1306/ssd1306_conf.h new file mode 100644 index 0000000000000000000000000000000000000000..06c17c6d334e53d94eed71dcb9f6c1a479551658 --- /dev/null +++ b/common/ssd1306/ssd1306_conf.h @@ -0,0 +1,27 @@ +#ifndef __SSD1306_CONF_H__ +#define __SSD1306_CONF_H__ + +// Choose a microcontroller family +// #define STM32F0 +//#define STM32F1 +//#define STM32F4 +//#define STM32L0 +//#define STM32L4 +//#define STM32F3 +//#define STM32H7 +//#define STM32F7 + +// Mirror the screen if needed +// #define SSD1306_MIRROR_VERT +// #define SSD1306_MIRROR_HORIZ + +// Set inverse color if needed +// # define SSD1306_INVERSE_COLOR + +// Include only needed fonts +#define SSD1306_INCLUDE_FONT_6x8 +#define SSD1306_INCLUDE_FONT_7x10 +#define SSD1306_INCLUDE_FONT_11x18 +#define SSD1306_INCLUDE_FONT_16x26 + +#endif /* __SSD1306_CONF_H__ */ diff --git a/common/ssd1306/ssd1306_drv.c b/common/ssd1306/ssd1306_drv.c new file mode 100644 index 0000000000000000000000000000000000000000..6b629bdc32d39381206857c3914b4425bb93f915 --- /dev/null +++ b/common/ssd1306/ssd1306_drv.c @@ -0,0 +1,507 @@ +#include "hi_spi.h" +#include "iot_spi.h" + +//定义I2C和SPI的宏 +#define SSD1306_USE_IIC 1 +#define SSD1306_USE_SPI 0 + +/* IIC方式控制SSD1306 */ +#if SSD1306_USE_IIC +#include "ssd1306_iic_drv.h" + +#define SSD1306_I2C_ADDR (0x3C <<1) +#define OLED_IIC_BAUDRATE 400000 +#endif + +/* SPI方式控制SSD1306 */ +#if SSD1306_USE_SPI +#include "ssd1306_spi_drv.h" + +#define U8_LEN 1 +#endif + + +//OLED显存总共分为8页 +//每页8行,一行128个像素点 +//OLED的显存 +//存放格式如下. +//[0]0 1 2 3 ... 127 (0~7)行 +//[1]0 1 2 3 ... 127 (8~15)行 +//[2]0 1 2 3 ... 127 (16~23)行 +//[3]0 1 2 3 ... 127 (24~31)行 +//[4]0 1 2 3 ... 127 (32~39)行 +//[5]0 1 2 3 ... 127 (40~47)行 +//[6]0 1 2 3 ... 127 (48~55)行 +//[7]0 1 2 3 ... 127 (56~63)行 + + +static unsigned char SSD1306_buffer[SSD1306_BUFFER_SIZE] = {0x00}; + + +/* IIC方式控制SSD1306 */ +#if SSD1306_USE_IIC +/******************************************************************* + * @name :void SSD1306InitGPIO(void) + * @date :2018-08-27 + * @function :初始化SSD1306的GPIO + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306InitGPIO(void) +{ + return; +} + +/******************************************************************* + * @name :void SSD1306IOInit(u8 i2cId) + * @date :2023-02-24 + * @function :初始化SSD1306的IIC,在此之前必须先初始化SSD1306的GPIO为IIC功能 + 即在调用本函数之前先调用iot_gpio_iic.h中的初始化接口函数 + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306InitIO(u8 i2cId) +{ + u32 ret = KP_ERR_SUCCESS; + + /* IIC初始化 */ + ret = IoTI2cInit(i2cId, OLED_IIC_BAUDRATE); + if (ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("IoTI2cInit failed :%#x \r\n", ret); + return; + } + + /* IIC设置波特率 */ + ret = IoTI2cSetBaudrate(i2cId, OLED_IIC_BAUDRATE); + if (ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("IoTI2cSetBaudrate failed :%#x \r\n", ret); + return; + } + + printf("SSD1306InitIO success \r\n"); +} + +/******************************************************************* + * @name :void OLED_Reset(void) + * @date :2018-08-27 + * @function :Reset OLED screen + * @parameters :dat:0-Display full black + 1-Display full white + * @retvalue :None +********************************************************************/ +void SSD1306Reset(void) +{ + /* for I2C - do nothing */ +} + +u32 SSD1306WriteBuf(u32 iicId, u8 *data, u32 byteLen) +{ + u32 ret = IoTI2cWrite(iicId, SSD1306_I2C_ADDR, data, byteLen); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTI2cWrite failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + +u32 SSD1306WriteCmd(u32 iicId, u8 cmd) +{ + u32 ret = KP_ERR_SUCCESS; + + u8 buffer[] = {SSD1306_CTRL_CMD, cmd}; + ret = SSD1306WriteBuf(iicId, SSD1306_I2C_ADDR, buffer, sizeof(buffer)); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("SSD1306WriteBuf failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + +/******************************************************************* + * @name :u32 SSD1306WriteData(u32 iicId, u8 *data, u32 byteLen) + * @date :2018-08-27 + * @function :Write a byte of content to the OLED screen + * @parameters :dat:Content to be written + dataType:0-write command + 1-write data + * @retvalue :None +********************************************************************/ +u32 SSD1306WriteData(u32 iicId, u8 *data, u32 byteLen) +{ + u32 ret = KP_ERR_SUCCESS; + u8 buff[byteLen * 2] = {0}; + + for (u32 i = 0; i < byteLen; i++) { + buff[i*2] = SSD1306_CTRL_DATA | SSD1306_MASK_CONT; + buff[i*2+1] = data[i]; + } + buff[(byteLen - 1) * 2] = SSD1306_CTRL_DATA; + ret = SSD1306WriteBuf(iicId, SSD1306_I2C_ADDR, buff, sizeof(buff)); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("SSD1306WriteBuf failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + +#endif + + +/* SPI方式控制SSD1306 */ +#if SSD1306_USE_SPI +/******************************************************************* + * @name :void SSD1306InitGPIO(void) + * @date :2018-08-27 + * @function :初始化SSD1306的GPIO + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306InitGPIO(void) +{ + return; +} + +/******************************************************************* + * @name :void SSD1306IOInit(u8 spiId) + * @date :2023-02-24 + * @function :初始化SSD1306的SPI,在此之前必须先初始化SSD1306的GPIO为SPI功能 + 即在调用本函数之前先调用iot_gpio_spi.h中的初始化接口函数 + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306InitIO(u8 spiId) +{ + u32 ret = KP_ERR_SUCCESS; + + /* 去初始化SPI */ + ret = IoTSpiDeinit(spiId); + if (ret != KP_ERR_SUCCESS) { + printf("IoTSpiDeinit failed :%#x \r\n", ret); + return; + } + + IotSpiCfgInitParam spiInitParam = {0}; + spiInitParam.isSlave = FALSE; + IotSpiCfgBasicInfo spiBasicInfo = {0}; + spiBasicInfo.cpol = HI_SPI_CFG_CLOCK_CPOL_0; + spiBasicInfo.cpha = HI_SPI_CFG_CLOCK_CPHA_0; + spiBasicInfo.framMode = HI_SPI_CFG_FRAM_MODE_MOTOROLA; + spiBasicInfo.dataWidth = HI_SPI_CFG_DATA_WIDTH_E_8BIT; + spiBasicInfo.endian = HI_SPI_CFG_ENDIAN_LITTLE; + spiBasicInfo.freq = 2000000; /* defaul freq 2000000 Hz */ + + //test_spi_printf("app_demo_spi_test_cmd_mw_sr Start"); + + ret = IoTSpiInit(spiId, spiInitParam, &spiBasicInfo); + if (ret != KP_ERR_SUCCESS) { + printf("IoTSpiInit failed :%#x \r\n", ret); + return; + } + + IoTSpiSetLoopBackMode(spiId, FALSE); + + osDelay(100); + + IoTSpiSetIrqMode(spiId, FALSE); + IoTSpiSetDmaMode(spiId, FALSE); + + osDelay(100); + + printf("SSD1306InitIO success \r\n"); +} + +/******************************************************************* + * @name :void OLED_Reset(void) + * @date :2018-08-27 + * @function :Reset OLED screen + * @parameters :dat:0-Display full black + 1-Display full white + * @retvalue :None +********************************************************************/ +void SSD1306Reset(void) +{ + SSD1306_RST_Set(); + delay_ms(100); + SSD1306_RST_Clr(); + delay_ms(100); + SSD1306_RST_Set(); + printf("SSD1306 reset \r\n"); +} + +u32 SSD1306WriteBuf(u32 spiId, u8 *data, u32 byteLen) +{ + u32 ret = IoTSpiHostWrite(spiId, data, byteLen); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("IoTSpiHostWrite failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + +/******************************************************************* + * @name :u32 SSD1306WriteCmd(u32 spiId, u8 cmd) + * @date :2018-08-27 + * @function :Write a byte of content to the OLED screen + * @parameters :dat:Content to be written + dataType:0-write command + 1-write data + * @retvalue :None +********************************************************************/ +u32 SSD1306WriteCmd(u32 spiId, u8 cmd) +{ + u32 ret = KP_ERR_SUCCESS; + + // 低电平写命令 + ret = SSD1306_DC_Clr(); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("SSD1306_DC_Clr failed :%#x \r\n", ret); + return ret; + } + + // 低电平片选 + ret = SSD1306_CS_Clr(); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("SSD1306_CS_Clr failed :%#x \r\n", ret); + return ret; + } + + // 写数据 + ret = SSD1306WriteBuf(spiId, cmd, U8_LEN); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("SSD1306WriteBuf failed :%#x \r\n", ret); + return ret; + } + + // 取消片选 + ret = SSD1306_CS_Set(); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("SSD1306_CS_Set failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} + +/******************************************************************* + * @name :u32 SSD1306WriteByte(u32 spiId, u8 dat, u32 dataType) + * @date :2018-08-27 + * @function :Write a byte of content to the OLED screen + * @parameters :dat:Content to be written + dataType:0-write command + 1-write data + * @retvalue :None +********************************************************************/ +u32 SSD1306WriteData(u32 spiId, u8 *data, u32 byteLen) +{ + u32 ret = KP_ERR_SUCCESS; + + // 高电平写数据 + ret = SSD1306_DC_Set(); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("SSD1306_DC_Set failed :%#x \r\n", ret); + return ret; + } + + // 低电平片选 + ret = SSD1306_CS_Clr(); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("SSD1306_CS_Clr failed :%#x \r\n", ret); + return ret; + } + + // 写数据 + ret = SSD1306WriteBuf(spiId, data, byteLen); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("Ssd1306WriteData failed :%#x \r\n", ret); + return ret; + } + + // 取消片选 + ret = SSD1306_CS_Set(); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("SSD1306_CS_Set failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} +#endif + +/* Fills the Screenbuffer with values from a given buffer of a fixed length */ +u32 SSD1306FillBuffer(u8* buf, u32 len) { + u32 ret = KP_ERR_FAILURE; + if (len <= SSD1306_BUFFER_SIZE) { + memcpy(SSD1306_Buffer, buf, len); + ret = KP_ERR_SUCCESS; + } + + return ret; +} + +/******************************************************************* + * @name :void OLED_Init(void) + * @date :2018-08-27 + * @function :initialise OLED SH1106 control IC + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306Init(void) +{ + //SSD1306InitGPIO(); //初始化GPIO + //delay_ms(200); + SSD1306Reset(); //复位OLED + printf("%s,%s,%d \r\n", __FILE__, __func__, __LINE__); + + // Wait for the screen to boot + osDelay(100); + +/**************初始化SSD1306*****************/ + SSD1306WRCmd(0xAE); /*display off*/ + SSD1306WRCmd(0x00); /*set lower column address*/ + SSD1306WRCmd(0x10); /*set higher column address*/ + SSD1306WRCmd(0x40); /*set display start line*/ + SSD1306WRCmd(0xB0); /*set page address*/ + SSD1306WRCmd(0x81); /*contract control*/ + SSD1306WRCmd(0xFF); /*128*/ + SSD1306WRCmd(0xA1); /*set segment remap*/ + SSD1306WRCmd(0xA6); /*normal / reverse*/ + SSD1306WRCmd(0xA8); /*multiplex ratio*/ + SSD1306WRCmd(0x3F); /*duty = 1/64*/ + SSD1306WRCmd(0xC8); /*Com scan direction*/ + SSD1306WRCmd(0xD3); /*set display offset*/ + SSD1306WRCmd(0x00); + SSD1306WRCmd(0xD5); /*set osc division*/ + SSD1306WRCmd(0x80); + SSD1306WRCmd(0xD9); /*set pre-charge period*/ + SSD1306WRCmd(0XF1); + SSD1306WRCmd(0xDA); /*set COM pins*/ + SSD1306WRCmd(0x12); + SSD1306WRCmd(0xDB); /*set vcomh*/ + SSD1306WRCmd(0x30); + SSD1306WRCmd(0x8D); /*set charge pump disable*/ + SSD1306WRCmd(0x14); + SSD1306WRCmd(0xAF); /*display ON*/ + + printf("%s,%s,%d \r\n", __FILE__, __func__, __LINE__); +} + +/******************************************************************* + * @name :void OLED_Set_Pos(unsigned char x, unsigned char y) + * @date :2018-08-27 + * @function :Set coordinates in the OLED screen + * @parameters :x:x coordinates + y:y coordinates + * @retvalue :None +********************************************************************/ +void SSD1306SetPos(unsigned char x, unsigned char y) +{ + SSD1306WRCmd(YLevel+y/PAGE_SIZE); + SSD1306WRCmd((((x+2)&0xf0)>>4)|0x10); + SSD1306WRCmd(((x+2)&0x0f)); +} + +/******************************************************************* + * @name :void OLED_Display_On(void) + * @date :2018-08-27 + * @function :Turn on OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOn(void) +{ + SSD1306WRCmd(0X8D); //SET DCDC命令 + SSD1306WRCmd(0X14); //DCDC ON + SSD1306WRCmd(0XAF); //DISPLAY ON +} + +/******************************************************************* + * @name :void OLED_Display_Off(void) + * @date :2018-08-27 + * @function :Turn off OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOff(void) +{ + SSD1306WRCmd(0X8D); //SET DCDC命令 + SSD1306WRCmd(0X10); //DCDC OFF + SSD1306WRCmd(0XAE); //DISPLAY OFF +} + +/******************************************************************* + * @name :void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color) + * @date :2018-08-27 + * @function :set the value of pixel to RAM + * @parameters :x:the x coordinates of pixel + y:the y coordinates of pixel + color:the color value of the point + 1-white + 0-black + * @retvalue :None +********************************************************************/ +void SSD1306SetPixel(unsigned char x, unsigned char y,unsigned char color) +{ + if(x >= SSD1306_WIDTH || y >= SSD1306_HEIGHT) { + // Don't write outside the buffer + return; + } + + if(color) + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] |= (1<<(y%PAGE_SIZE))&0xff; + } + else + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] &= ~((1<<(y%PAGE_SIZE))&0xff); + } +} + +/******************************************************************* + * @name :void OLED_Display(void) + * @date :2018-08-27 + * @function :Display in OLED screen + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306Display(void) +{ + u8 i,n; + for(i=0; i +0x3800, 0x4400, 0x0400, 0x0800, 0x1000, 0x1000, 0x0000, 0x1000, 0x0000, 0x0000, // ? +0x3800, 0x4400, 0x4C00, 0x5400, 0x5C00, 0x4000, 0x4000, 0x3800, 0x0000, 0x0000, // @ +0x1000, 0x2800, 0x2800, 0x2800, 0x2800, 0x7C00, 0x4400, 0x4400, 0x0000, 0x0000, // A +0x7800, 0x4400, 0x4400, 0x7800, 0x4400, 0x4400, 0x4400, 0x7800, 0x0000, 0x0000, // B +0x3800, 0x4400, 0x4000, 0x4000, 0x4000, 0x4000, 0x4400, 0x3800, 0x0000, 0x0000, // C +0x7000, 0x4800, 0x4400, 0x4400, 0x4400, 0x4400, 0x4800, 0x7000, 0x0000, 0x0000, // D +0x7C00, 0x4000, 0x4000, 0x7C00, 0x4000, 0x4000, 0x4000, 0x7C00, 0x0000, 0x0000, // E +0x7C00, 0x4000, 0x4000, 0x7800, 0x4000, 0x4000, 0x4000, 0x4000, 0x0000, 0x0000, // F +0x3800, 0x4400, 0x4000, 0x4000, 0x5C00, 0x4400, 0x4400, 0x3800, 0x0000, 0x0000, // G +0x4400, 0x4400, 0x4400, 0x7C00, 0x4400, 0x4400, 0x4400, 0x4400, 0x0000, 0x0000, // H +0x3800, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x3800, 0x0000, 0x0000, // I +0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x4400, 0x3800, 0x0000, 0x0000, // J +0x4400, 0x4800, 0x5000, 0x6000, 0x5000, 0x4800, 0x4800, 0x4400, 0x0000, 0x0000, // K +0x4000, 0x4000, 0x4000, 0x4000, 0x4000, 0x4000, 0x4000, 0x7C00, 0x0000, 0x0000, // L +0x4400, 0x6C00, 0x6C00, 0x5400, 0x4400, 0x4400, 0x4400, 0x4400, 0x0000, 0x0000, // M +0x4400, 0x6400, 0x6400, 0x5400, 0x5400, 0x4C00, 0x4C00, 0x4400, 0x0000, 0x0000, // N +0x3800, 0x4400, 0x4400, 0x4400, 0x4400, 0x4400, 0x4400, 0x3800, 0x0000, 0x0000, // O +0x7800, 0x4400, 0x4400, 0x4400, 0x7800, 0x4000, 0x4000, 0x4000, 0x0000, 0x0000, // P +0x3800, 0x4400, 0x4400, 0x4400, 0x4400, 0x4400, 0x5400, 0x3800, 0x0400, 0x0000, // Q +0x7800, 0x4400, 0x4400, 0x4400, 0x7800, 0x4800, 0x4800, 0x4400, 0x0000, 0x0000, // R +0x3800, 0x4400, 0x4000, 0x3000, 0x0800, 0x0400, 0x4400, 0x3800, 0x0000, 0x0000, // S +0x7C00, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x0000, 0x0000, // T +0x4400, 0x4400, 0x4400, 0x4400, 0x4400, 0x4400, 0x4400, 0x3800, 0x0000, 0x0000, // U +0x4400, 0x4400, 0x4400, 0x2800, 0x2800, 0x2800, 0x1000, 0x1000, 0x0000, 0x0000, // V +0x4400, 0x4400, 0x5400, 0x5400, 0x5400, 0x6C00, 0x2800, 0x2800, 0x0000, 0x0000, // W +0x4400, 0x2800, 0x2800, 0x1000, 0x1000, 0x2800, 0x2800, 0x4400, 0x0000, 0x0000, // X +0x4400, 0x4400, 0x2800, 0x2800, 0x1000, 0x1000, 0x1000, 0x1000, 0x0000, 0x0000, // Y +0x7C00, 0x0400, 0x0800, 0x1000, 0x1000, 0x2000, 0x4000, 0x7C00, 0x0000, 0x0000, // Z +0x1800, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1800, // [ +0x2000, 0x2000, 0x1000, 0x1000, 0x1000, 0x1000, 0x0800, 0x0800, 0x0000, 0x0000, /* \ */ +0x3000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x3000, // ] +0x1000, 0x2800, 0x2800, 0x4400, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ^ +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFE00, // _ +0x2000, 0x1000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ` +0x0000, 0x0000, 0x3800, 0x4400, 0x3C00, 0x4400, 0x4C00, 0x3400, 0x0000, 0x0000, // a +0x4000, 0x4000, 0x5800, 0x6400, 0x4400, 0x4400, 0x6400, 0x5800, 0x0000, 0x0000, // b +0x0000, 0x0000, 0x3800, 0x4400, 0x4000, 0x4000, 0x4400, 0x3800, 0x0000, 0x0000, // c +0x0400, 0x0400, 0x3400, 0x4C00, 0x4400, 0x4400, 0x4C00, 0x3400, 0x0000, 0x0000, // d +0x0000, 0x0000, 0x3800, 0x4400, 0x7C00, 0x4000, 0x4400, 0x3800, 0x0000, 0x0000, // e +0x0C00, 0x1000, 0x7C00, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x0000, 0x0000, // f +0x0000, 0x0000, 0x3400, 0x4C00, 0x4400, 0x4400, 0x4C00, 0x3400, 0x0400, 0x7800, // g +0x4000, 0x4000, 0x5800, 0x6400, 0x4400, 0x4400, 0x4400, 0x4400, 0x0000, 0x0000, // h +0x1000, 0x0000, 0x7000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x0000, 0x0000, // i +0x1000, 0x0000, 0x7000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0xE000, // j +0x4000, 0x4000, 0x4800, 0x5000, 0x6000, 0x5000, 0x4800, 0x4400, 0x0000, 0x0000, // k +0x7000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x0000, 0x0000, // l +0x0000, 0x0000, 0x7800, 0x5400, 0x5400, 0x5400, 0x5400, 0x5400, 0x0000, 0x0000, // m +0x0000, 0x0000, 0x5800, 0x6400, 0x4400, 0x4400, 0x4400, 0x4400, 0x0000, 0x0000, // n +0x0000, 0x0000, 0x3800, 0x4400, 0x4400, 0x4400, 0x4400, 0x3800, 0x0000, 0x0000, // o +0x0000, 0x0000, 0x5800, 0x6400, 0x4400, 0x4400, 0x6400, 0x5800, 0x4000, 0x4000, // p +0x0000, 0x0000, 0x3400, 0x4C00, 0x4400, 0x4400, 0x4C00, 0x3400, 0x0400, 0x0400, // q +0x0000, 0x0000, 0x5800, 0x6400, 0x4000, 0x4000, 0x4000, 0x4000, 0x0000, 0x0000, // r +0x0000, 0x0000, 0x3800, 0x4400, 0x3000, 0x0800, 0x4400, 0x3800, 0x0000, 0x0000, // s +0x2000, 0x2000, 0x7800, 0x2000, 0x2000, 0x2000, 0x2000, 0x1800, 0x0000, 0x0000, // t +0x0000, 0x0000, 0x4400, 0x4400, 0x4400, 0x4400, 0x4C00, 0x3400, 0x0000, 0x0000, // u +0x0000, 0x0000, 0x4400, 0x4400, 0x2800, 0x2800, 0x2800, 0x1000, 0x0000, 0x0000, // v +0x0000, 0x0000, 0x5400, 0x5400, 0x5400, 0x6C00, 0x2800, 0x2800, 0x0000, 0x0000, // w +0x0000, 0x0000, 0x4400, 0x2800, 0x1000, 0x1000, 0x2800, 0x4400, 0x0000, 0x0000, // x +0x0000, 0x0000, 0x4400, 0x4400, 0x2800, 0x2800, 0x1000, 0x1000, 0x1000, 0x6000, // y +0x0000, 0x0000, 0x7C00, 0x0800, 0x1000, 0x2000, 0x4000, 0x7C00, 0x0000, 0x0000, // z +0x1800, 0x1000, 0x1000, 0x1000, 0x2000, 0x2000, 0x1000, 0x1000, 0x1000, 0x1800, // { +0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x1000, // | +0x3000, 0x1000, 0x1000, 0x1000, 0x0800, 0x0800, 0x1000, 0x1000, 0x1000, 0x3000, // } +0x0000, 0x0000, 0x0000, 0x7400, 0x4C00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ~ +}; +#endif + +#ifdef SSD1306_INCLUDE_FONT_11x18 +static const uint16_t Font11x18 [] = { +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // sp +0x0000, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0000, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, // ! +0x0000, 0x1B00, 0x1B00, 0x1B00, 0x1B00, 0x1B00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // " +0x0000, 0x1980, 0x1980, 0x1980, 0x1980, 0x7FC0, 0x7FC0, 0x1980, 0x3300, 0x7FC0, 0x7FC0, 0x3300, 0x3300, 0x3300, 0x3300, 0x0000, 0x0000, 0x0000, // # +0x0000, 0x1E00, 0x3F00, 0x7580, 0x6580, 0x7400, 0x3C00, 0x1E00, 0x0700, 0x0580, 0x6580, 0x6580, 0x7580, 0x3F00, 0x1E00, 0x0400, 0x0400, 0x0000, // $ +0x0000, 0x7000, 0xD800, 0xD840, 0xD8C0, 0xD980, 0x7300, 0x0600, 0x0C00, 0x1B80, 0x36C0, 0x66C0, 0x46C0, 0x06C0, 0x0380, 0x0000, 0x0000, 0x0000, // % +0x0000, 0x1E00, 0x3F00, 0x3300, 0x3300, 0x3300, 0x1E00, 0x0C00, 0x3CC0, 0x66C0, 0x6380, 0x6180, 0x6380, 0x3EC0, 0x1C80, 0x0000, 0x0000, 0x0000, // & +0x0000, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ' +0x0080, 0x0100, 0x0300, 0x0600, 0x0600, 0x0400, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0400, 0x0600, 0x0600, 0x0300, 0x0100, 0x0080, // ( +0x2000, 0x1000, 0x1800, 0x0C00, 0x0C00, 0x0400, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0400, 0x0C00, 0x0C00, 0x1800, 0x1000, 0x2000, // ) +0x0000, 0x0C00, 0x2D00, 0x3F00, 0x1E00, 0x3300, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // * +0x0000, 0x0000, 0x0000, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0xFFC0, 0xFFC0, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // + +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0C00, 0x0C00, 0x0400, 0x0400, 0x0800, // , +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1E00, 0x1E00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // - +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, // . +0x0000, 0x0300, 0x0300, 0x0300, 0x0600, 0x0600, 0x0600, 0x0600, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x1800, 0x1800, 0x1800, 0x0000, 0x0000, 0x0000, // / +0x0000, 0x1E00, 0x3F00, 0x3300, 0x6180, 0x6180, 0x6180, 0x6D80, 0x6D80, 0x6180, 0x6180, 0x6180, 0x3300, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // 0 +0x0000, 0x0600, 0x0E00, 0x1E00, 0x3600, 0x2600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, // 1 +0x0000, 0x1E00, 0x3F00, 0x7380, 0x6180, 0x6180, 0x0180, 0x0300, 0x0600, 0x0C00, 0x1800, 0x3000, 0x6000, 0x7F80, 0x7F80, 0x0000, 0x0000, 0x0000, // 2 +0x0000, 0x1C00, 0x3E00, 0x6300, 0x6300, 0x0300, 0x0E00, 0x0E00, 0x0300, 0x0180, 0x0180, 0x6180, 0x7380, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // 3 +0x0000, 0x0600, 0x0E00, 0x0E00, 0x1E00, 0x1E00, 0x1600, 0x3600, 0x3600, 0x6600, 0x7F80, 0x7F80, 0x0600, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, // 4 +0x0000, 0x7F00, 0x7F00, 0x6000, 0x6000, 0x6000, 0x6E00, 0x7F00, 0x6380, 0x0180, 0x0180, 0x6180, 0x7380, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // 5 +0x0000, 0x1E00, 0x3F00, 0x3380, 0x6180, 0x6000, 0x6E00, 0x7F00, 0x7380, 0x6180, 0x6180, 0x6180, 0x3380, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // 6 +0x0000, 0x7F80, 0x7F80, 0x0180, 0x0300, 0x0300, 0x0600, 0x0600, 0x0C00, 0x0C00, 0x0C00, 0x0800, 0x1800, 0x1800, 0x1800, 0x0000, 0x0000, 0x0000, // 7 +0x0000, 0x1E00, 0x3F00, 0x6380, 0x6180, 0x6180, 0x2100, 0x1E00, 0x3F00, 0x6180, 0x6180, 0x6180, 0x6180, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // 8 +0x0000, 0x1E00, 0x3F00, 0x7300, 0x6180, 0x6180, 0x6180, 0x7380, 0x3F80, 0x1D80, 0x0180, 0x6180, 0x7300, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // 9 +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, // : +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0C00, 0x0C00, 0x0400, 0x0400, 0x0800, // ; +0x0000, 0x0000, 0x0000, 0x0000, 0x0080, 0x0380, 0x0E00, 0x3800, 0x6000, 0x3800, 0x0E00, 0x0380, 0x0080, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // < +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x7F80, 0x7F80, 0x0000, 0x0000, 0x7F80, 0x7F80, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // = +0x0000, 0x0000, 0x0000, 0x0000, 0x4000, 0x7000, 0x1C00, 0x0700, 0x0180, 0x0700, 0x1C00, 0x7000, 0x4000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // > +0x0000, 0x1F00, 0x3F80, 0x71C0, 0x60C0, 0x00C0, 0x01C0, 0x0380, 0x0700, 0x0E00, 0x0C00, 0x0C00, 0x0000, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, // ? +0x0000, 0x1E00, 0x3F00, 0x3180, 0x7180, 0x6380, 0x6F80, 0x6D80, 0x6D80, 0x6F80, 0x6780, 0x6000, 0x3200, 0x3E00, 0x1C00, 0x0000, 0x0000, 0x0000, // @ +0x0000, 0x0E00, 0x0E00, 0x1B00, 0x1B00, 0x1B00, 0x1B00, 0x3180, 0x3180, 0x3F80, 0x3F80, 0x3180, 0x60C0, 0x60C0, 0x60C0, 0x0000, 0x0000, 0x0000, // A +0x0000, 0x7C00, 0x7E00, 0x6300, 0x6300, 0x6300, 0x6300, 0x7E00, 0x7E00, 0x6300, 0x6180, 0x6180, 0x6380, 0x7F00, 0x7E00, 0x0000, 0x0000, 0x0000, // B +0x0000, 0x1E00, 0x3F00, 0x3180, 0x6180, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6180, 0x3180, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // C +0x0000, 0x7C00, 0x7F00, 0x6300, 0x6380, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6300, 0x6300, 0x7E00, 0x7C00, 0x0000, 0x0000, 0x0000, // D +0x0000, 0x7F80, 0x7F80, 0x6000, 0x6000, 0x6000, 0x6000, 0x7F00, 0x7F00, 0x6000, 0x6000, 0x6000, 0x6000, 0x7F80, 0x7F80, 0x0000, 0x0000, 0x0000, // E +0x0000, 0x7F80, 0x7F80, 0x6000, 0x6000, 0x6000, 0x6000, 0x7F00, 0x7F00, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x0000, 0x0000, 0x0000, // F +0x0000, 0x1E00, 0x3F00, 0x3180, 0x6180, 0x6000, 0x6000, 0x6000, 0x6380, 0x6380, 0x6180, 0x6180, 0x3180, 0x3F80, 0x1E00, 0x0000, 0x0000, 0x0000, // G +0x0000, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x7F80, 0x7F80, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x0000, 0x0000, 0x0000, // H +0x0000, 0x3F00, 0x3F00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x3F00, 0x3F00, 0x0000, 0x0000, 0x0000, // I +0x0000, 0x0180, 0x0180, 0x0180, 0x0180, 0x0180, 0x0180, 0x0180, 0x0180, 0x0180, 0x6180, 0x6180, 0x7380, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // J +0x0000, 0x60C0, 0x6180, 0x6300, 0x6600, 0x6600, 0x6C00, 0x7800, 0x7C00, 0x6600, 0x6600, 0x6300, 0x6180, 0x6180, 0x60C0, 0x0000, 0x0000, 0x0000, // K +0x0000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x7F80, 0x7F80, 0x0000, 0x0000, 0x0000, // L +0x0000, 0x71C0, 0x71C0, 0x7BC0, 0x7AC0, 0x6AC0, 0x6AC0, 0x6EC0, 0x64C0, 0x60C0, 0x60C0, 0x60C0, 0x60C0, 0x60C0, 0x60C0, 0x0000, 0x0000, 0x0000, // M +0x0000, 0x7180, 0x7180, 0x7980, 0x7980, 0x7980, 0x6D80, 0x6D80, 0x6D80, 0x6580, 0x6780, 0x6780, 0x6780, 0x6380, 0x6380, 0x0000, 0x0000, 0x0000, // N +0x0000, 0x1E00, 0x3F00, 0x3300, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x3300, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // O +0x0000, 0x7E00, 0x7F00, 0x6380, 0x6180, 0x6180, 0x6180, 0x6380, 0x7F00, 0x7E00, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x0000, 0x0000, 0x0000, // P +0x0000, 0x1E00, 0x3F00, 0x3300, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6580, 0x6780, 0x3300, 0x3F80, 0x1E40, 0x0000, 0x0000, 0x0000, // Q +0x0000, 0x7E00, 0x7F00, 0x6380, 0x6180, 0x6180, 0x6380, 0x7F00, 0x7E00, 0x6600, 0x6300, 0x6300, 0x6180, 0x6180, 0x60C0, 0x0000, 0x0000, 0x0000, // R +0x0000, 0x0E00, 0x1F00, 0x3180, 0x3180, 0x3000, 0x3800, 0x1E00, 0x0700, 0x0380, 0x6180, 0x6180, 0x3180, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // S +0x0000, 0xFFC0, 0xFFC0, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, // T +0x0000, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x7380, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // U +0x0000, 0x60C0, 0x60C0, 0x60C0, 0x3180, 0x3180, 0x3180, 0x1B00, 0x1B00, 0x1B00, 0x1B00, 0x0E00, 0x0E00, 0x0E00, 0x0400, 0x0000, 0x0000, 0x0000, // V +0x0000, 0xC0C0, 0xC0C0, 0xC0C0, 0xC0C0, 0xC0C0, 0xCCC0, 0x4C80, 0x4C80, 0x5E80, 0x5280, 0x5280, 0x7380, 0x6180, 0x6180, 0x0000, 0x0000, 0x0000, // W +0x0000, 0xC0C0, 0x6080, 0x6180, 0x3300, 0x3B00, 0x1E00, 0x0C00, 0x0C00, 0x1E00, 0x1F00, 0x3B00, 0x7180, 0x6180, 0xC0C0, 0x0000, 0x0000, 0x0000, // X +0x0000, 0xC0C0, 0x6180, 0x6180, 0x3300, 0x3300, 0x1E00, 0x1E00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, // Y +0x0000, 0x3F80, 0x3F80, 0x0180, 0x0300, 0x0300, 0x0600, 0x0C00, 0x0C00, 0x1800, 0x1800, 0x3000, 0x6000, 0x7F80, 0x7F80, 0x0000, 0x0000, 0x0000, // Z +0x0F00, 0x0F00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0F00, 0x0F00, // [ +0x0000, 0x1800, 0x1800, 0x1800, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0600, 0x0600, 0x0600, 0x0600, 0x0300, 0x0300, 0x0300, 0x0000, 0x0000, 0x0000, /* \ */ +0x1E00, 0x1E00, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x1E00, 0x1E00, // ] +0x0000, 0x0C00, 0x0C00, 0x1E00, 0x1200, 0x3300, 0x3300, 0x6180, 0x6180, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ^ +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xFFE0, 0x0000, // _ +0x0000, 0x3800, 0x1800, 0x0C00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ` +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1F00, 0x3F80, 0x6180, 0x0180, 0x1F80, 0x3F80, 0x6180, 0x6380, 0x7F80, 0x38C0, 0x0000, 0x0000, 0x0000, // a +0x0000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6E00, 0x7F00, 0x7380, 0x6180, 0x6180, 0x6180, 0x6180, 0x7380, 0x7F00, 0x6E00, 0x0000, 0x0000, 0x0000, // b +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1E00, 0x3F00, 0x7380, 0x6180, 0x6000, 0x6000, 0x6180, 0x7380, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // c +0x0000, 0x0180, 0x0180, 0x0180, 0x0180, 0x1D80, 0x3F80, 0x7380, 0x6180, 0x6180, 0x6180, 0x6180, 0x7380, 0x3F80, 0x1D80, 0x0000, 0x0000, 0x0000, // d +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1E00, 0x3F00, 0x7300, 0x6180, 0x7F80, 0x7F80, 0x6000, 0x7180, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // e +0x0000, 0x07C0, 0x0FC0, 0x0C00, 0x0C00, 0x7F80, 0x7F80, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0000, 0x0000, 0x0000, // f +0x0000, 0x0000, 0x0000, 0x0000, 0x1D80, 0x3F80, 0x7380, 0x6180, 0x6180, 0x6180, 0x6180, 0x7380, 0x3F80, 0x1D80, 0x0180, 0x6380, 0x7F00, 0x3E00, // g +0x0000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6F00, 0x7F80, 0x7180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x0000, 0x0000, 0x0000, // h +0x0000, 0x0600, 0x0600, 0x0000, 0x0000, 0x3E00, 0x3E00, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, // i +0x0600, 0x0600, 0x0000, 0x0000, 0x3E00, 0x3E00, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x4600, 0x7E00, 0x3C00, // j +0x0000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6180, 0x6300, 0x6600, 0x6C00, 0x7C00, 0x7600, 0x6300, 0x6300, 0x6180, 0x60C0, 0x0000, 0x0000, 0x0000, // k +0x0000, 0x3E00, 0x3E00, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0000, 0x0000, 0x0000, // l +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xDD80, 0xFFC0, 0xCEC0, 0xCCC0, 0xCCC0, 0xCCC0, 0xCCC0, 0xCCC0, 0xCCC0, 0xCCC0, 0x0000, 0x0000, 0x0000, // m +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x6F00, 0x7F80, 0x7180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x0000, 0x0000, 0x0000, // n +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1E00, 0x3F00, 0x7380, 0x6180, 0x6180, 0x6180, 0x6180, 0x7380, 0x3F00, 0x1E00, 0x0000, 0x0000, 0x0000, // o +0x0000, 0x0000, 0x0000, 0x0000, 0x6E00, 0x7F00, 0x7380, 0x6180, 0x6180, 0x6180, 0x6180, 0x7380, 0x7F00, 0x6E00, 0x6000, 0x6000, 0x6000, 0x6000, // p +0x0000, 0x0000, 0x0000, 0x0000, 0x1D80, 0x3F80, 0x7380, 0x6180, 0x6180, 0x6180, 0x6180, 0x7380, 0x3F80, 0x1D80, 0x0180, 0x0180, 0x0180, 0x0180, // q +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x6700, 0x3F80, 0x3900, 0x3000, 0x3000, 0x3000, 0x3000, 0x3000, 0x3000, 0x3000, 0x0000, 0x0000, 0x0000, // r +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1E00, 0x3F80, 0x6180, 0x6000, 0x7F00, 0x3F80, 0x0180, 0x6180, 0x7F00, 0x1E00, 0x0000, 0x0000, 0x0000, // s +0x0000, 0x0000, 0x0800, 0x1800, 0x1800, 0x7F00, 0x7F00, 0x1800, 0x1800, 0x1800, 0x1800, 0x1800, 0x1800, 0x1F80, 0x0F80, 0x0000, 0x0000, 0x0000, // t +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6180, 0x6380, 0x7F80, 0x3D80, 0x0000, 0x0000, 0x0000, // u +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x60C0, 0x3180, 0x3180, 0x3180, 0x1B00, 0x1B00, 0x1B00, 0x0E00, 0x0E00, 0x0600, 0x0000, 0x0000, 0x0000, // v +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xDD80, 0xDD80, 0xDD80, 0x5500, 0x5500, 0x5500, 0x7700, 0x7700, 0x2200, 0x2200, 0x0000, 0x0000, 0x0000, // w +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x6180, 0x3300, 0x3300, 0x1E00, 0x0C00, 0x0C00, 0x1E00, 0x3300, 0x3300, 0x6180, 0x0000, 0x0000, 0x0000, // x +0x0000, 0x0000, 0x0000, 0x0000, 0x6180, 0x6180, 0x3180, 0x3300, 0x3300, 0x1B00, 0x1B00, 0x1B00, 0x0E00, 0x0E00, 0x0E00, 0x1C00, 0x7C00, 0x7000, // y +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x7FC0, 0x7FC0, 0x0180, 0x0300, 0x0600, 0x0C00, 0x1800, 0x3000, 0x7FC0, 0x7FC0, 0x0000, 0x0000, 0x0000, // z +0x0380, 0x0780, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0E00, 0x1C00, 0x1C00, 0x0E00, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0780, 0x0380, // { +0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, 0x0600, // | +0x3800, 0x3C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0E00, 0x0700, 0x0700, 0x0E00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x0C00, 0x3C00, 0x3800, // } +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x3880, 0x7F80, 0x4700, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ~ +}; +#endif +#ifdef SSD1306_INCLUDE_FONT_16x26 +static const uint16_t Font16x26 [] = { +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [ ] +0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03C0,0x03C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x0000,0x0000,0x0000,0x03E0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [!] +0x1E3C,0x1E3C,0x1E3C,0x1E3C,0x1E3C,0x1E3C,0x1E3C,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = ["] +0x01CE,0x03CE,0x03DE,0x039E,0x039C,0x079C,0x3FFF,0x7FFF,0x0738,0x0F38,0x0F78,0x0F78,0x0E78,0xFFFF,0xFFFF,0x1EF0,0x1CF0,0x1CE0,0x3CE0,0x3DE0,0x39E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [#] +0x03FC,0x0FFE,0x1FEE,0x1EE0,0x1EE0,0x1EE0,0x1EE0,0x1FE0,0x0FE0,0x07E0,0x03F0,0x01FC,0x01FE,0x01FE,0x01FE,0x01FE,0x01FE,0x01FE,0x3DFE,0x3FFC,0x0FF0,0x01E0,0x01E0,0x0000,0x0000,0x0000, // Ascii = [$] +0x3E03,0xF707,0xE78F,0xE78E,0xE39E,0xE3BC,0xE7B8,0xE7F8,0xF7F0,0x3FE0,0x01C0,0x03FF,0x07FF,0x07F3,0x0FF3,0x1EF3,0x3CF3,0x38F3,0x78F3,0xF07F,0xE03F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [%] +0x07E0,0x0FF8,0x0F78,0x1F78,0x1F78,0x1F78,0x0F78,0x0FF0,0x0FE0,0x1F80,0x7FC3,0xFBC3,0xF3E7,0xF1F7,0xF0F7,0xF0FF,0xF07F,0xF83E,0x7C7F,0x3FFF,0x1FEF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [&] +0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03C0,0x01C0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = ['] +0x003F,0x007C,0x01F0,0x01E0,0x03C0,0x07C0,0x0780,0x0780,0x0F80,0x0F00,0x0F00,0x0F00,0x0F00,0x0F00,0x0F00,0x0F80,0x0780,0x0780,0x07C0,0x03C0,0x01E0,0x01F0,0x007C,0x003F,0x000F,0x0000, // Ascii = [(] +0x7E00,0x1F00,0x07C0,0x03C0,0x01E0,0x01F0,0x00F0,0x00F0,0x00F8,0x0078,0x0078,0x0078,0x0078,0x0078,0x0078,0x00F8,0x00F0,0x00F0,0x01F0,0x01E0,0x03C0,0x07C0,0x1F00,0x7E00,0x7800,0x0000, // Ascii = [)] +0x03E0,0x03C0,0x01C0,0x39CE,0x3FFF,0x3F7F,0x0320,0x0370,0x07F8,0x0F78,0x1F3C,0x0638,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [*] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0xFFFF,0xFFFF,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [+] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x03E0,0x03E0,0x03E0,0x03E0,0x01E0,0x01E0,0x01E0,0x01C0,0x0380, // Ascii = [,] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3FFE,0x3FFE,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [-] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x03E0,0x03E0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [.] +0x000F,0x000F,0x001E,0x001E,0x003C,0x003C,0x0078,0x0078,0x00F0,0x00F0,0x01E0,0x01E0,0x03C0,0x03C0,0x0780,0x0780,0x0F00,0x0F00,0x1E00,0x1E00,0x3C00,0x3C00,0x7800,0x7800,0xF000,0x0000, // Ascii = [/] +0x07F0,0x0FF8,0x1F7C,0x3E3E,0x3C1E,0x7C1F,0x7C1F,0x780F,0x780F,0x780F,0x780F,0x780F,0x780F,0x780F,0x7C1F,0x7C1F,0x3C1E,0x3E3E,0x1F7C,0x0FF8,0x07F0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [0] +0x00F0,0x07F0,0x3FF0,0x3FF0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x3FFF,0x3FFF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [1] +0x0FE0,0x3FF8,0x3C7C,0x003C,0x003E,0x003E,0x003E,0x003C,0x003C,0x007C,0x00F8,0x01F0,0x03E0,0x07C0,0x0780,0x0F00,0x1E00,0x3E00,0x3C00,0x3FFE,0x3FFE,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [2] +0x0FF0,0x1FF8,0x1C7C,0x003E,0x003E,0x003E,0x003C,0x003C,0x00F8,0x0FF0,0x0FF8,0x007C,0x003E,0x001E,0x001E,0x001E,0x001E,0x003E,0x1C7C,0x1FF8,0x1FE0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [3] +0x0078,0x00F8,0x00F8,0x01F8,0x03F8,0x07F8,0x07F8,0x0F78,0x1E78,0x1E78,0x3C78,0x7878,0x7878,0xFFFF,0xFFFF,0x0078,0x0078,0x0078,0x0078,0x0078,0x0078,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [4] +0x1FFC,0x1FFC,0x1FFC,0x1E00,0x1E00,0x1E00,0x1E00,0x1E00,0x1FE0,0x1FF8,0x00FC,0x007C,0x003E,0x003E,0x001E,0x003E,0x003E,0x003C,0x1C7C,0x1FF8,0x1FE0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [5] +0x01FC,0x07FE,0x0F8E,0x1F00,0x1E00,0x3E00,0x3C00,0x3C00,0x3DF8,0x3FFC,0x7F3E,0x7E1F,0x3C0F,0x3C0F,0x3C0F,0x3C0F,0x3E0F,0x1E1F,0x1F3E,0x0FFC,0x03F0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [6] +0x3FFF,0x3FFF,0x3FFF,0x000F,0x001E,0x001E,0x003C,0x0038,0x0078,0x00F0,0x00F0,0x01E0,0x01E0,0x03C0,0x03C0,0x0780,0x0F80,0x0F80,0x0F00,0x1F00,0x1F00,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [7] +0x07F8,0x0FFC,0x1F3E,0x1E1E,0x3E1E,0x3E1E,0x1E1E,0x1F3C,0x0FF8,0x07F0,0x0FF8,0x1EFC,0x3E3E,0x3C1F,0x7C1F,0x7C0F,0x7C0F,0x3C1F,0x3F3E,0x1FFC,0x07F0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [8] +0x07F0,0x0FF8,0x1E7C,0x3C3E,0x3C1E,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x3C1F,0x3E3F,0x1FFF,0x07EF,0x001F,0x001E,0x001E,0x003E,0x003C,0x38F8,0x3FF0,0x1FE0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [9] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x03E0,0x03E0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x03E0,0x03E0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [:] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x03E0,0x03E0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x03E0,0x03E0,0x03E0,0x03E0,0x01E0,0x01E0,0x01E0,0x03C0,0x0380, // Ascii = [;] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0003,0x000F,0x003F,0x00FC,0x03F0,0x0FC0,0x3F00,0xFE00,0x3F00,0x0FC0,0x03F0,0x00FC,0x003F,0x000F,0x0003,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [<] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xFFFF,0xFFFF,0x0000,0x0000,0x0000,0xFFFF,0xFFFF,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [=] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xE000,0xF800,0x7E00,0x1F80,0x07E0,0x01F8,0x007E,0x001F,0x007E,0x01F8,0x07E0,0x1F80,0x7E00,0xF800,0xE000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [>] +0x1FF0,0x3FFC,0x383E,0x381F,0x381F,0x001E,0x001E,0x003C,0x0078,0x00F0,0x01E0,0x03C0,0x03C0,0x07C0,0x07C0,0x0000,0x0000,0x0000,0x07C0,0x07C0,0x07C0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [?] +0x03F8,0x0FFE,0x1F1E,0x3E0F,0x3C7F,0x78FF,0x79EF,0x73C7,0xF3C7,0xF38F,0xF38F,0xF38F,0xF39F,0xF39F,0x73FF,0x7BFF,0x79F7,0x3C00,0x1F1C,0x0FFC,0x03F8,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [@] +0x0000,0x0000,0x0000,0x03E0,0x03E0,0x07F0,0x07F0,0x07F0,0x0F78,0x0F78,0x0E7C,0x1E3C,0x1E3C,0x3C3E,0x3FFE,0x3FFF,0x781F,0x780F,0xF00F,0xF007,0xF007,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [A] +0x0000,0x0000,0x0000,0x3FF8,0x3FFC,0x3C3E,0x3C1E,0x3C1E,0x3C1E,0x3C3E,0x3C7C,0x3FF0,0x3FF8,0x3C7E,0x3C1F,0x3C1F,0x3C0F,0x3C0F,0x3C1F,0x3FFE,0x3FF8,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [B] +0x0000,0x0000,0x0000,0x01FF,0x07FF,0x1F87,0x3E00,0x3C00,0x7C00,0x7800,0x7800,0x7800,0x7800,0x7800,0x7C00,0x7C00,0x3E00,0x3F00,0x1F83,0x07FF,0x01FF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [C] +0x0000,0x0000,0x0000,0x7FF0,0x7FFC,0x787E,0x781F,0x781F,0x780F,0x780F,0x780F,0x780F,0x780F,0x780F,0x780F,0x780F,0x781F,0x781E,0x787E,0x7FF8,0x7FE0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [D] +0x0000,0x0000,0x0000,0x3FFF,0x3FFF,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3FFE,0x3FFE,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3FFF,0x3FFF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [E] +0x0000,0x0000,0x0000,0x1FFF,0x1FFF,0x1E00,0x1E00,0x1E00,0x1E00,0x1E00,0x1E00,0x1FFF,0x1FFF,0x1E00,0x1E00,0x1E00,0x1E00,0x1E00,0x1E00,0x1E00,0x1E00,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [F] +0x0000,0x0000,0x0000,0x03FE,0x0FFF,0x1F87,0x3E00,0x7C00,0x7C00,0x7800,0xF800,0xF800,0xF87F,0xF87F,0x780F,0x7C0F,0x7C0F,0x3E0F,0x1F8F,0x0FFF,0x03FE,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [G] +0x0000,0x0000,0x0000,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7FFF,0x7FFF,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x7C1F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [H] +0x0000,0x0000,0x0000,0x3FFF,0x3FFF,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x3FFF,0x3FFF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [I] +0x0000,0x0000,0x0000,0x1FFC,0x1FFC,0x007C,0x007C,0x007C,0x007C,0x007C,0x007C,0x007C,0x007C,0x007C,0x007C,0x007C,0x0078,0x0078,0x38F8,0x3FF0,0x3FC0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [J] +0x0000,0x0000,0x0000,0x3C1F,0x3C1E,0x3C3C,0x3C78,0x3CF0,0x3DE0,0x3FE0,0x3FC0,0x3F80,0x3FC0,0x3FE0,0x3DF0,0x3CF0,0x3C78,0x3C7C,0x3C3E,0x3C1F,0x3C0F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [K] +0x0000,0x0000,0x0000,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3FFF,0x3FFF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [L] +0x0000,0x0000,0x0000,0xF81F,0xFC1F,0xFC1F,0xFE3F,0xFE3F,0xFE3F,0xFF7F,0xFF77,0xFF77,0xF7F7,0xF7E7,0xF3E7,0xF3E7,0xF3C7,0xF007,0xF007,0xF007,0xF007,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [M] +0x0000,0x0000,0x0000,0x7C0F,0x7C0F,0x7E0F,0x7F0F,0x7F0F,0x7F8F,0x7F8F,0x7FCF,0x7BEF,0x79EF,0x79FF,0x78FF,0x78FF,0x787F,0x783F,0x783F,0x781F,0x781F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [N] +0x0000,0x0000,0x0000,0x07F0,0x1FFC,0x3E3E,0x7C1F,0x780F,0x780F,0xF80F,0xF80F,0xF80F,0xF80F,0xF80F,0xF80F,0x780F,0x780F,0x7C1F,0x3E3E,0x1FFC,0x07F0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [O] +0x0000,0x0000,0x0000,0x3FFC,0x3FFF,0x3E1F,0x3E0F,0x3E0F,0x3E0F,0x3E0F,0x3E1F,0x3E3F,0x3FFC,0x3FF0,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x3E00,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [P] +0x0000,0x0000,0x0000,0x07F0,0x1FFC,0x3E3E,0x7C1F,0x780F,0x780F,0xF80F,0xF80F,0xF80F,0xF80F,0xF80F,0xF80F,0x780F,0x780F,0x7C1F,0x3E3E,0x1FFC,0x07F8,0x007C,0x003F,0x000F,0x0003,0x0000, // Ascii = [Q] +0x0000,0x0000,0x0000,0x3FF0,0x3FFC,0x3C7E,0x3C3E,0x3C1E,0x3C1E,0x3C3E,0x3C3C,0x3CFC,0x3FF0,0x3FE0,0x3DF0,0x3CF8,0x3C7C,0x3C3E,0x3C1E,0x3C1F,0x3C0F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [R] +0x0000,0x0000,0x0000,0x07FC,0x1FFE,0x3E0E,0x3C00,0x3C00,0x3C00,0x3E00,0x1FC0,0x0FF8,0x03FE,0x007F,0x001F,0x000F,0x000F,0x201F,0x3C3E,0x3FFC,0x1FF0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [S] +0x0000,0x0000,0x0000,0xFFFF,0xFFFF,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [T] +0x0000,0x0000,0x0000,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x7C0F,0x3C1E,0x3C1E,0x3E3E,0x1FFC,0x07F0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [U] +0x0000,0x0000,0x0000,0xF007,0xF007,0xF807,0x780F,0x7C0F,0x3C1E,0x3C1E,0x3E1E,0x1E3C,0x1F3C,0x1F78,0x0F78,0x0FF8,0x07F0,0x07F0,0x07F0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [V] +0x0000,0x0000,0x0000,0xE003,0xF003,0xF003,0xF007,0xF3E7,0xF3E7,0xF3E7,0x73E7,0x7BF7,0x7FF7,0x7FFF,0x7F7F,0x7F7F,0x7F7E,0x3F7E,0x3E3E,0x3E3E,0x3E3E,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [W] +0x0000,0x0000,0x0000,0xF807,0x7C0F,0x3E1E,0x3E3E,0x1F3C,0x0FF8,0x07F0,0x07E0,0x03E0,0x03E0,0x07F0,0x0FF8,0x0F7C,0x1E7C,0x3C3E,0x781F,0x780F,0xF00F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [X] +0x0000,0x0000,0x0000,0xF807,0x7807,0x7C0F,0x3C1E,0x3E1E,0x1F3C,0x0F78,0x0FF8,0x07F0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [Y] +0x0000,0x0000,0x0000,0x7FFF,0x7FFF,0x000F,0x001F,0x003E,0x007C,0x00F8,0x00F0,0x01E0,0x03E0,0x07C0,0x0F80,0x0F00,0x1E00,0x3E00,0x7C00,0x7FFF,0x7FFF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [Z] +0x07FF,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x07FF,0x07FF,0x0000, // Ascii = [[] +0x7800,0x7800,0x3C00,0x3C00,0x1E00,0x1E00,0x0F00,0x0F00,0x0780,0x0780,0x03C0,0x03C0,0x01E0,0x01E0,0x00F0,0x00F0,0x0078,0x0078,0x003C,0x003C,0x001E,0x001E,0x000F,0x000F,0x0007,0x0000, // Ascii = [\] +0x7FF0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x00F0,0x7FF0,0x7FF0,0x0000, // Ascii = []] +0x00C0,0x01C0,0x01C0,0x03E0,0x03E0,0x07F0,0x07F0,0x0778,0x0F78,0x0F38,0x1E3C,0x1E3C,0x3C1E,0x3C1E,0x380F,0x780F,0x7807,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [^] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xFFFF,0xFFFF,0x0000,0x0000,0x0000, // Ascii = [_] +0x00F0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [`] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0FF8,0x3FFC,0x3C7C,0x003E,0x003E,0x003E,0x07FE,0x1FFE,0x3E3E,0x7C3E,0x783E,0x7C3E,0x7C7E,0x3FFF,0x1FCF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [a] +0x3C00,0x3C00,0x3C00,0x3C00,0x3C00,0x3C00,0x3DF8,0x3FFE,0x3F3E,0x3E1F,0x3C0F,0x3C0F,0x3C0F,0x3C0F,0x3C0F,0x3C0F,0x3C1F,0x3C1E,0x3F3E,0x3FFC,0x3BF0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [b] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x03FE,0x0FFF,0x1F87,0x3E00,0x3E00,0x3C00,0x7C00,0x7C00,0x7C00,0x3C00,0x3E00,0x3E00,0x1F87,0x0FFF,0x03FE,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [c] +0x001F,0x001F,0x001F,0x001F,0x001F,0x001F,0x07FF,0x1FFF,0x3E3F,0x3C1F,0x7C1F,0x7C1F,0x7C1F,0x781F,0x781F,0x7C1F,0x7C1F,0x3C3F,0x3E7F,0x1FFF,0x0FDF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [d] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x03F8,0x0FFC,0x1F3E,0x3E1E,0x3C1F,0x7C1F,0x7FFF,0x7FFF,0x7C00,0x7C00,0x3C00,0x3E00,0x1F07,0x0FFF,0x03FE,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [e] +0x01FF,0x03E1,0x03C0,0x07C0,0x07C0,0x07C0,0x7FFF,0x7FFF,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x07C0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [f] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x07EF,0x1FFF,0x3E7F,0x3C1F,0x7C1F,0x7C1F,0x781F,0x781F,0x781F,0x7C1F,0x7C1F,0x3C3F,0x3E7F,0x1FFF,0x0FDF,0x001E,0x001E,0x001E,0x387C,0x3FF8, // Ascii = [g] +0x3C00,0x3C00,0x3C00,0x3C00,0x3C00,0x3C00,0x3DFC,0x3FFE,0x3F9E,0x3F1F,0x3E1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [h] +0x01F0,0x01F0,0x0000,0x0000,0x0000,0x0000,0x7FE0,0x7FE0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [i] +0x00F8,0x00F8,0x0000,0x0000,0x0000,0x0000,0x3FF8,0x3FF8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F8,0x00F0,0x71F0,0x7FE0, // Ascii = [j] +0x3C00,0x3C00,0x3C00,0x3C00,0x3C00,0x3C00,0x3C1F,0x3C3E,0x3C7C,0x3CF8,0x3DF0,0x3DE0,0x3FC0,0x3FC0,0x3FE0,0x3DF0,0x3CF8,0x3C7C,0x3C3E,0x3C1F,0x3C1F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [k] +0x7FF0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x01F0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [l] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xF79E,0xFFFF,0xFFFF,0xFFFF,0xFBE7,0xF9E7,0xF1C7,0xF1C7,0xF1C7,0xF1C7,0xF1C7,0xF1C7,0xF1C7,0xF1C7,0xF1C7,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [m] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3DFC,0x3FFE,0x3F9E,0x3F1F,0x3E1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x3C1F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [n] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x07F0,0x1FFC,0x3E3E,0x3C1F,0x7C1F,0x780F,0x780F,0x780F,0x780F,0x780F,0x7C1F,0x3C1F,0x3E3E,0x1FFC,0x07F0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [o] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3DF8,0x3FFE,0x3F3E,0x3E1F,0x3C0F,0x3C0F,0x3C0F,0x3C0F,0x3C0F,0x3C0F,0x3C1F,0x3E1E,0x3F3E,0x3FFC,0x3FF8,0x3C00,0x3C00,0x3C00,0x3C00,0x3C00, // Ascii = [p] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x07EE,0x1FFE,0x3E7E,0x3C1E,0x7C1E,0x781E,0x781E,0x781E,0x781E,0x781E,0x7C1E,0x7C3E,0x3E7E,0x1FFE,0x0FDE,0x001E,0x001E,0x001E,0x001E,0x001E, // Ascii = [q] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1F7F,0x1FFF,0x1FE7,0x1FC7,0x1F87,0x1F00,0x1F00,0x1F00,0x1F00,0x1F00,0x1F00,0x1F00,0x1F00,0x1F00,0x1F00,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [r] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x07FC,0x1FFE,0x1E0E,0x3E00,0x3E00,0x3F00,0x1FE0,0x07FC,0x00FE,0x003E,0x001E,0x001E,0x3C3E,0x3FFC,0x1FF0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [s] +0x0000,0x0000,0x0000,0x0780,0x0780,0x0780,0x7FFF,0x7FFF,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x0780,0x07C0,0x03FF,0x01FF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [t] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3C1E,0x3C1E,0x3C1E,0x3C1E,0x3C1E,0x3C1E,0x3C1E,0x3C1E,0x3C1E,0x3C1E,0x3C3E,0x3C7E,0x3EFE,0x1FFE,0x0FDE,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [u] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xF007,0x780F,0x780F,0x3C1E,0x3C1E,0x3E1E,0x1E3C,0x1E3C,0x0F78,0x0F78,0x0FF0,0x07F0,0x07F0,0x03E0,0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [v] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xF003,0xF1E3,0xF3E3,0xF3E7,0xF3F7,0xF3F7,0x7FF7,0x7F77,0x7F7F,0x7F7F,0x7F7F,0x3E3E,0x3E3E,0x3E3E,0x3E3E,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [w] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x7C0F,0x3E1E,0x3E3C,0x1F3C,0x0FF8,0x07F0,0x07F0,0x03E0,0x07F0,0x07F8,0x0FF8,0x1E7C,0x3E3E,0x3C1F,0x781F,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [x] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xF807,0x780F,0x7C0F,0x3C1E,0x3C1E,0x1E3C,0x1E3C,0x1F3C,0x0F78,0x0FF8,0x07F0,0x07F0,0x03E0,0x03E0,0x03C0,0x03C0,0x03C0,0x0780,0x0F80,0x7F00, // Ascii = [y] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3FFF,0x3FFF,0x001F,0x003E,0x007C,0x00F8,0x01F0,0x03E0,0x07C0,0x0F80,0x1F00,0x1E00,0x3C00,0x7FFF,0x7FFF,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [z] +0x01FE,0x03E0,0x03C0,0x03C0,0x03C0,0x03C0,0x01E0,0x01E0,0x01E0,0x01C0,0x03C0,0x3F80,0x3F80,0x03C0,0x01C0,0x01E0,0x01E0,0x01E0,0x03C0,0x03C0,0x03C0,0x03C0,0x03E0,0x01FE,0x007E,0x0000, // Ascii = [{] +0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x01C0,0x0000, // Ascii = [|] +0x3FC0,0x03E0,0x01E0,0x01E0,0x01E0,0x01E0,0x01C0,0x03C0,0x03C0,0x01C0,0x01E0,0x00FE,0x00FE,0x01E0,0x01C0,0x03C0,0x03C0,0x01C0,0x01E0,0x01E0,0x01E0,0x01E0,0x03E0,0x3FC0,0x3F00,0x0000, // Ascii = [}] +0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3F07,0x7FC7,0x73E7,0xF1FF,0xF07E,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, // Ascii = [~] +}; +#endif +#ifdef SSD1306_INCLUDE_FONT_6x8 +static const uint16_t Font6x8 [] = { +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // sp +0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x0000, 0x2000, 0x0000, // ! +0x5000, 0x5000, 0x5000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // " +0x5000, 0x5000, 0xf800, 0x5000, 0xf800, 0x5000, 0x5000, 0x0000, // # +0x2000, 0x7800, 0xa000, 0x7000, 0x2800, 0xf000, 0x2000, 0x0000, // $ +0xc000, 0xc800, 0x1000, 0x2000, 0x4000, 0x9800, 0x1800, 0x0000, // % +0x4000, 0xa000, 0xa000, 0x4000, 0xa800, 0x9000, 0x6800, 0x0000, // & +0x3000, 0x3000, 0x2000, 0x4000, 0x0000, 0x0000, 0x0000, 0x0000, // ' +0x1000, 0x2000, 0x4000, 0x4000, 0x4000, 0x2000, 0x1000, 0x0000, // ( +0x4000, 0x2000, 0x1000, 0x1000, 0x1000, 0x2000, 0x4000, 0x0000, // ) +0x2000, 0xa800, 0x7000, 0xf800, 0x7000, 0xa800, 0x2000, 0x0000, // * +0x0000, 0x2000, 0x2000, 0xf800, 0x2000, 0x2000, 0x0000, 0x0000, // + +0x0000, 0x0000, 0x0000, 0x0000, 0x3000, 0x3000, 0x2000, 0x0000, // , +0x0000, 0x0000, 0x0000, 0xf800, 0x0000, 0x0000, 0x0000, 0x0000, // - +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x3000, 0x3000, 0x0000, // . +0x0000, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000, 0x0000, 0x0000, // / +0x7000, 0x8800, 0x9800, 0xa800, 0xc800, 0x8800, 0x7000, 0x0000, // 0 +0x2000, 0x6000, 0x2000, 0x2000, 0x2000, 0x2000, 0x7000, 0x0000, // 1 +0x7000, 0x8800, 0x0800, 0x7000, 0x8000, 0x8000, 0xf800, 0x0000, // 2 +0xf800, 0x0800, 0x1000, 0x3000, 0x0800, 0x8800, 0x7000, 0x0000, // 3 +0x1000, 0x3000, 0x5000, 0x9000, 0xf800, 0x1000, 0x1000, 0x0000, // 4 +0xf800, 0x8000, 0xf000, 0x0800, 0x0800, 0x8800, 0x7000, 0x0000, // 5 +0x3800, 0x4000, 0x8000, 0xf000, 0x8800, 0x8800, 0x7000, 0x0000, // 6 +0xf800, 0x0800, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000, 0x0000, // 7 +0x7000, 0x8800, 0x8800, 0x7000, 0x8800, 0x8800, 0x7000, 0x0000, // 8 +0x7000, 0x8800, 0x8800, 0x7800, 0x0800, 0x1000, 0xe000, 0x0000, // 9 +0x0000, 0x0000, 0x2000, 0x0000, 0x2000, 0x0000, 0x0000, 0x0000, // : +0x0000, 0x0000, 0x2000, 0x0000, 0x2000, 0x2000, 0x4000, 0x0000, // ; +0x0800, 0x1000, 0x2000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0000, // < +0x0000, 0x0000, 0xf800, 0x0000, 0xf800, 0x0000, 0x0000, 0x0000, // = +0x4000, 0x2000, 0x1000, 0x0800, 0x1000, 0x2000, 0x4000, 0x0000, // > +0x7000, 0x8800, 0x0800, 0x3000, 0x2000, 0x0000, 0x2000, 0x0000, // ? +0x7000, 0x8800, 0xa800, 0xb800, 0xb000, 0x8000, 0x7800, 0x0000, // @ +0x2000, 0x5000, 0x8800, 0x8800, 0xf800, 0x8800, 0x8800, 0x0000, // A +0xf000, 0x8800, 0x8800, 0xf000, 0x8800, 0x8800, 0xf000, 0x0000, // B +0x7000, 0x8800, 0x8000, 0x8000, 0x8000, 0x8800, 0x7000, 0x0000, // C +0xf000, 0x8800, 0x8800, 0x8800, 0x8800, 0x8800, 0xf000, 0x0000, // D +0xf800, 0x8000, 0x8000, 0xf000, 0x8000, 0x8000, 0xf800, 0x0000, // E +0xf800, 0x8000, 0x8000, 0xf000, 0x8000, 0x8000, 0x8000, 0x0000, // F +0x7800, 0x8800, 0x8000, 0x8000, 0x9800, 0x8800, 0x7800, 0x0000, // G +0x8800, 0x8800, 0x8800, 0xf800, 0x8800, 0x8800, 0x8800, 0x0000, // H +0x7000, 0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x7000, 0x0000, // I +0x3800, 0x1000, 0x1000, 0x1000, 0x1000, 0x9000, 0x6000, 0x0000, // J +0x8800, 0x9000, 0xa000, 0xc000, 0xa000, 0x9000, 0x8800, 0x0000, // K +0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0xf800, 0x0000, // L +0x8800, 0xd800, 0xa800, 0xa800, 0xa800, 0x8800, 0x8800, 0x0000, // M +0x8800, 0x8800, 0xc800, 0xa800, 0x9800, 0x8800, 0x8800, 0x0000, // N +0x7000, 0x8800, 0x8800, 0x8800, 0x8800, 0x8800, 0x7000, 0x0000, // O +0xf000, 0x8800, 0x8800, 0xf000, 0x8000, 0x8000, 0x8000, 0x0000, // P +0x7000, 0x8800, 0x8800, 0x8800, 0xa800, 0x9000, 0x6800, 0x0000, // Q +0xf000, 0x8800, 0x8800, 0xf000, 0xa000, 0x9000, 0x8800, 0x0000, // R +0x7000, 0x8800, 0x8000, 0x7000, 0x0800, 0x8800, 0x7000, 0x0000, // S +0xf800, 0xa800, 0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x0000, // T +0x8800, 0x8800, 0x8800, 0x8800, 0x8800, 0x8800, 0x7000, 0x0000, // U +0x8800, 0x8800, 0x8800, 0x8800, 0x8800, 0x5000, 0x2000, 0x0000, // V +0x8800, 0x8800, 0x8800, 0xa800, 0xa800, 0xa800, 0x5000, 0x0000, // W +0x8800, 0x8800, 0x5000, 0x2000, 0x5000, 0x8800, 0x8800, 0x0000, // X +0x8800, 0x8800, 0x5000, 0x2000, 0x2000, 0x2000, 0x2000, 0x0000, // Y +0xf800, 0x0800, 0x1000, 0x7000, 0x4000, 0x8000, 0xf800, 0x0000, // Z +0x7800, 0x4000, 0x4000, 0x4000, 0x4000, 0x4000, 0x7800, 0x0000, // [ +0x0000, 0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0000, 0x0000, /* \ */ +0x7800, 0x0800, 0x0800, 0x0800, 0x0800, 0x0800, 0x7800, 0x0000, // ] +0x2000, 0x5000, 0x8800, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ^ +0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xf800, 0x0000, // _ +0x6000, 0x6000, 0x2000, 0x1000, 0x0000, 0x0000, 0x0000, 0x0000, // ` +0x0000, 0x0000, 0x6000, 0x1000, 0x7000, 0x9000, 0x7800, 0x0000, // a +0x8000, 0x8000, 0xb000, 0xc800, 0x8800, 0xc800, 0xb000, 0x0000, // b +0x0000, 0x0000, 0x7000, 0x8800, 0x8000, 0x8800, 0x7000, 0x0000, // c +0x0800, 0x0800, 0x6800, 0x9800, 0x8800, 0x9800, 0x6800, 0x0000, // d +0x0000, 0x0000, 0x7000, 0x8800, 0xf800, 0x8000, 0x7000, 0x0000, // e +0x1000, 0x2800, 0x2000, 0x7000, 0x2000, 0x2000, 0x2000, 0x0000, // f +0x0000, 0x0000, 0x7000, 0x9800, 0x9800, 0x6800, 0x0800, 0x0000, // g +0x8000, 0x8000, 0xb000, 0xc800, 0x8800, 0x8800, 0x8800, 0x0000, // h +0x2000, 0x0000, 0x6000, 0x2000, 0x2000, 0x2000, 0x7000, 0x0000, // i +0x1000, 0x0000, 0x1000, 0x1000, 0x1000, 0x9000, 0x6000, 0x0000, // j +0x8000, 0x8000, 0x9000, 0xa000, 0xc000, 0xa000, 0x9000, 0x0000, // k +0x6000, 0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x7000, 0x0000, // l +0x0000, 0x0000, 0xd000, 0xa800, 0xa800, 0xa800, 0xa800, 0x0000, // m +0x0000, 0x0000, 0xb000, 0xc800, 0x8800, 0x8800, 0x8800, 0x0000, // n +0x0000, 0x0000, 0x7000, 0x8800, 0x8800, 0x8800, 0x7000, 0x0000, // o +0x0000, 0x0000, 0xb000, 0xc800, 0xc800, 0xb000, 0x8000, 0x0000, // p +0x0000, 0x0000, 0x6800, 0x9800, 0x9800, 0x6800, 0x0800, 0x0000, // q +0x0000, 0x0000, 0xb000, 0xc800, 0x8000, 0x8000, 0x8000, 0x0000, // r +0x0000, 0x0000, 0x7800, 0x8000, 0x7000, 0x0800, 0xf000, 0x0000, // s +0x2000, 0x2000, 0xf800, 0x2000, 0x2000, 0x2800, 0x1000, 0x0000, // t +0x0000, 0x0000, 0x8800, 0x8800, 0x8800, 0x9800, 0x6800, 0x0000, // u +0x0000, 0x0000, 0x8800, 0x8800, 0x8800, 0x5000, 0x2000, 0x0000, // v +0x0000, 0x0000, 0x8800, 0x8800, 0xa800, 0xa800, 0x5000, 0x0000, // w +0x0000, 0x0000, 0x8800, 0x5000, 0x2000, 0x5000, 0x8800, 0x0000, // x +0x0000, 0x0000, 0x8800, 0x8800, 0x7800, 0x0800, 0x8800, 0x0000, // y +0x0000, 0x0000, 0xf800, 0x1000, 0x2000, 0x4000, 0xf800, 0x0000, // z +0x1000, 0x2000, 0x2000, 0x4000, 0x2000, 0x2000, 0x1000, 0x0000, // { +0x2000, 0x2000, 0x2000, 0x0000, 0x2000, 0x2000, 0x2000, 0x0000, // | +0x4000, 0x2000, 0x2000, 0x1000, 0x2000, 0x2000, 0x4000, 0x0000, // } +0x4000, 0xa800, 0x1000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, // ~ +}; +#endif + +#ifdef SSD1306_INCLUDE_FONT_6x8 +FontDef Font_6x8 = {6,8,Font6x8}; +#endif +#ifdef SSD1306_INCLUDE_FONT_7x10 +FontDef Font_7x10 = {7,10,Font7x10}; +#endif +#ifdef SSD1306_INCLUDE_FONT_11x18 +FontDef Font_11x18 = {11,18,Font11x18}; +#endif +#ifdef SSD1306_INCLUDE_FONT_16x26 +FontDef Font_16x26 = {16,26,Font16x26}; +#endif + diff --git a/common/ssd1306/ssd1306_fonts.h b/common/ssd1306/ssd1306_fonts.h new file mode 100644 index 0000000000000000000000000000000000000000..e7cb5786fd1543c71cf382784e8e1d0f6cf70f82 --- /dev/null +++ b/common/ssd1306/ssd1306_fonts.h @@ -0,0 +1,26 @@ +#ifndef __SSD1306_FONTS_H__ +#define __SSD1306_FONTS_H__ + +#include +#include "ssd1306_conf.h" + +typedef struct { + const uint8_t FontWidth; /*!< Font width in pixels */ + uint8_t FontHeight; /*!< Font height in pixels */ + const uint16_t *data; /*!< Pointer to data font data array */ +} FontDef; + +#ifdef SSD1306_INCLUDE_FONT_6x8 +extern FontDef Font_6x8; +#endif +#ifdef SSD1306_INCLUDE_FONT_7x10 +extern FontDef Font_7x10; +#endif +#ifdef SSD1306_INCLUDE_FONT_11x18 +extern FontDef Font_11x18; +#endif +#ifdef SSD1306_INCLUDE_FONT_16x26 +extern FontDef Font_16x26; +#endif +#endif // __SSD1306_FONTS_H__ + diff --git a/common/ssd1306/ssd1306_gui.c b/common/ssd1306/ssd1306_gui.c new file mode 100644 index 0000000000000000000000000000000000000000..33797a0b2e672eac8fcdeea8eb5ce0cb9b50d2e0 --- /dev/null +++ b/common/ssd1306/ssd1306_gui.c @@ -0,0 +1,859 @@ +#include "ssd1306_drv.h" +#include "oled_font.h" +#include "ssd1306_gui.h" +#include "iot_base_type.h" + +/******************************************************************* + * @name :void GUI_DrawPoint(u8 x,u8 y,u8 color) + * @date :2018-08-27 + * @function :draw a point in LCD screen + * @parameters :x:the x coordinate of the point + y:the y coordinate of the point + color:the color value of the point + 1-white + 0-black + * @retvalue :None +********************************************************************/ +void GUI_DrawPoint(u8 x, u8 y, u8 color) +{ + SSD1306SetPixel(x, y, color); + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/******************************************************************* + * @name :void GUI_Fill(u8 sx,u8 sy,u8 ex,u8 ey,u8 color) + * @date :2018-08-27 + * @function :fill the specified area + * @parameters :sx:the bebinning x coordinate of the specified area + sy:the bebinning y coordinate of the specified area + ex:the ending x coordinate of the specified area + ey:the ending y coordinate of the specified area + color:the color value of the the specified area + 1-white + 0-black + * @retvalue :None +********************************************************************/ +void GUI_Fill(u8 sx, u8 sy, u8 ex, u8 ey, u8 color) +{ + u8 i = 0, j = 0; + u8 width = ex - sx + 1; //得到填充的宽度 + u8 height = ey - sy + 1; //高度 + for(i = 0; i < height; i++) + { + for(j = 0; j < width; j++) + { + SSD1306SetPixel(sx + j, sy + i, color); + } + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/******************************************************************* + * @name :void GUI_DrawLine(u8 x1, u8 y1, u8 x2, u8 y2,u8 color) + * @date :2018-08-27 + * @function :Draw a line between two points + * @parameters :x1:the bebinning x coordinate of the line + y1:the bebinning y coordinate of the line + x2:the ending x coordinate of the line + y2:the ending y coordinate of the line + color:the color value of the line + 1-white + 0-black + * @retvalue :None +********************************************************************/ +void GUI_DrawLine(u8 x1, u8 y1, u8 x2, u8 y2, u8 color) +{ + u16 t = 0; + int xerr = 0, yerr = 0, delta_x = 0, delta_y = 0, distance = 0; + int incx = 0, incy = 0, uRow = 0, uCol = 0; + + delta_x = x2 - x1; ////计算坐标增量 + delta_y = y2 - y1; + uRow = x1; + uCol = y1; + if (delta_x > 0) + incx = 1; //设置单步方向 + else if (delta_x == 0) + incx = 0; //垂直线 + else { + incx = -1; + delta_x = -delta_x; + } + + if (delta_y > 0) + incy = 1; + else if (delta_y == 0) + incy = 0; //水平线 + else { + incy = -1; + delta_y = -delta_y; + } + + if ( delta_x > delta_y) + distance = delta_x; //选取基本增量坐标轴 + else + distance = delta_y; + + for (t = 0; t <= distance + 1; t++) //画线输出 + { + SSD1306SetPixel(uRow, uCol, color); + xerr += delta_x; + yerr += delta_y; + if (xerr > distance) + { + xerr -= distance; + uRow += incx; + } + if (yerr > distance) + { + yerr -= distance; + uCol += incy; + } + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/***************************************************************************** + * @name :void GUI_DrawRectangle(u8 x1, u8 y1, u8 x2, u8 y2,u8 color) + * @date :2018-08-27 + * @function :Draw a rectangle + * @parameters :x1:the bebinning x coordinate of the rectangle + y1:the bebinning y coordinate of the rectangle + x2:the ending x coordinate of the rectangle + y2:the ending y coordinate of the rectangle + color:the color value of the rectangle + 1-white + 0-black + * @retvalue :None +******************************************************************************/ +void GUI_DrawRectangle(u8 x1, u8 y1, u8 x2, u8 y2, u8 color) +{ + GUI_DrawLine(x1, y1, x2, y1, color); + GUI_DrawLine(x1, y1, x1, y2, color); + GUI_DrawLine(x1, y2, x2, y2, color); + GUI_DrawLine(x2, y1, x2, y2, color); +} + +/***************************************************************************** + * @name :void GUI_FillRectangle(u8 x1, u8 y1, u8 x2, u8 y2,u8 color) + * @date :2018-08-27 + * @function :Filled a rectangle + * @parameters :x1:the bebinning x coordinate of the filled rectangle + y1:the bebinning y coordinate of the filled rectangle + x2:the ending x coordinate of the filled rectangle + y2:the ending y coordinate of the filled rectangle + color:the color value of the rectangle + 1-white + 0-black + * @retvalue :None +******************************************************************************/ +void GUI_FillRectangle(u8 x1, u8 y1, u8 x2, u8 y2, u8 color) +{ + GUI_Fill(x1, y1, x2, y2, color); +} + +/***************************************************************************** + * @name :static void _draw_circle_8(u8 xc, u8 yc, u8 x, u8 y, u8 color) + * @date :2018-08-27 + * @function :8 symmetry circle drawing algorithm (internal call) + * @parameters :xc:the x coordinate of the Circular center + yc:the y coordinate of the Circular center + x:the x coordinate relative to the Circular center + y:the y coordinate relative to the Circular center + color:the color value of the rectangle + 1-white + 0-black + * @retvalue :None +******************************************************************************/ +static void _draw_circle_8(u8 xc, u8 yc, u8 x, u8 y, u8 color) +{ + OLED_Set_Pixel(xc + x, yc + y, color); + OLED_Set_Pixel(xc - x, yc + y, color); + OLED_Set_Pixel(xc + x, yc - y, color); + OLED_Set_Pixel(xc - x, yc - y, color); + OLED_Set_Pixel(xc + y, yc + x, color); + OLED_Set_Pixel(xc - y, yc + x, color); + OLED_Set_Pixel(xc + y, yc - x, color); + OLED_Set_Pixel(xc - y, yc - x, color); +} + +/***************************************************************************** + * @name :void GUI_DrawCircle(u8 xc, u8 yc, u8 color, u8 r) + * @date :2018-08-27 + * @function :Draw a circle of specified size at a specified location + * @parameters :xc:the x coordinate of the Circular center + yc:the y coordinate of the Circular center + r:Circular radius + color:the color value of the rectangle + 1-white + 0-black + * @retvalue :None +******************************************************************************/ +void GUI_DrawCircle(u8 xc, u8 yc, u8 color, u8 r) +{ + int x = 0, y = r, d; + d = 3 - 2 * r; + while (x <= y) + { + _draw_circle_8(xc, yc, x, y, color); + if (d < 0) + { + d = d + 4 * x + 6; + } + else + { + d = d + 4 * (x - y) + 10; + y--; + } + x++; + } + + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/***************************************************************************** + * @name :void GUI_FillCircle(u8 xc, u8 yc, u8 color, u8 r) + * @date :2018-08-27 + * @function :Fill a circle of specified size at a specified location + * @parameters :xc:the x coordinate of the Circular center + yc:the y coordinate of the Circular center + r:Circular radius + color:the color value of the rectangle + 1-white + 0-black + * @retvalue :None +******************************************************************************/ +void GUI_FillCircle(u8 xc, u8 yc, u8 color, u8 r) +{ + int x = 0, y = r, yi, d; + d = 3 - 2 * r; + while (x <= y) + { + for (yi = x; yi <= y; yi++) + { + _draw_circle_8(xc, yc, x, yi, color); + } + + if (d < 0) + { + d = d + 4 * x + 6; + } + else + { + d = d + 4 * (x - y) + 10; + y--; + } + x++; + } + + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/********************************************************************************** + * @name :void GUI_DrawTriangel(u8 x0,u8 y0,u8 x1,u8 y1,u8 x2,u8 y2,u8 color) + * @date :2018-08-27 + * @function :Draw a triangle at a specified position + * @parameters :x0:the bebinning x coordinate of the triangular edge + y0:the bebinning y coordinate of the triangular edge + x1:the vertex x coordinate of the triangular + y1:the vertex y coordinate of the triangular + x2:the ending x coordinate of the triangular edge + y2:the ending y coordinate of the triangular edge + color:the color value of the rectangle + 1-white + 0-black + * @retvalue :None +***********************************************************************************/ +void GUI_DrawTriangel(u8 x0, u8 y0, u8 x1, u8 y1, u8 x2, u8 y2, u8 color) +{ + GUI_DrawLine(x0, y0, x1, y1, color); + GUI_DrawLine(x1, y1, x2, y2, color); + GUI_DrawLine(x2, y2, x0, y0, color); +} + +/***************************************************************************** + * @name :static void _swap(u8 *a, u8 *b) + * @date :2018-08-27 + * @function :Exchange two numbers(internal call) + * @parameters :a:the address of the first number + b:the address of the second number + * @retvalue :None +******************************************************************************/ +static void _swap(u8 *a, u8 *b) +{ + u16 tmp; + tmp = *a; + *a = *b; + *b = tmp; +} + +/***************************************************************************** + * @name :static void _draw_h_line(u8 x0,u8 x1,u8 y,u8 color) + * @date :2018-08-27 + * @function :draw a horizontal line in RAM(internal call) + * @parameters :x0:the bebinning x coordinate of the horizontal line + x1:the ending x coordinate of the horizontal line + y:the y coordinate of the horizontal line + color:the color value of the rectangle + 1-white + 0-black + * @retvalue :None +******************************************************************************/ +static void _draw_h_line(u8 x0, u8 x1, u8 y, u8 color) +{ + u8 i = 0; + for(i = x0; i <= x1; i++) + { + SSD1306SetPixel(i, y, color); + } +} + +/***************************************************************************** + * @name :void GUI_FillTriangel(u8 x0,u8 y0,u8 x1,u8 y1,u8 x2,u8 y2,u8 color) + * @date :2018-08-27 + * @function :filling a triangle at a specified position + * @parameters :x0:the bebinning x coordinate of the triangular edge + y0:the bebinning y coordinate of the triangular edge + x1:the vertex x coordinate of the triangular + y1:the vertex y coordinate of the triangular + x2:the ending x coordinate of the triangular edge + y2:the ending y coordinate of the triangular edge + color:the color value of the rectangle + 1-white + 0-black + * @retvalue :None +******************************************************************************/ +void GUI_FillTriangel(u8 x0, u8 y0, u8 x1, u8 y1, u8 x2, u8 y2, u8 color) +{ + u8 a, b, y, last; + int dx01, dy01, dx02, dy02, dx12, dy12; + long sa = 0; + long sb = 0; + if (y0 > y1) + { + _swap(&y0,&y1); + _swap(&x0,&x1); + } + if (y1 > y2) + { + _swap(&y2,&y1); + _swap(&x2,&x1); + } + if (y0 > y1) + { + _swap(&y0,&y1); + _swap(&x0,&x1); + } + if(y0 == y2) + { + a = b = x0; + if(x1 < a) + { + a = x1; + } + else if(x1 > b) + { + b = x1; + } + if(x2 < a) + { + a = x2; + } + else if(x2 > b) + { + b = x2; + } + _draw_h_line(a, b, y0, color); + return; + } + dx01 = x1 - x0; + dy01 = y1 - y0; + dx02 = x2 - x0; + dy02 = y2 - y0; + dx12 = x2 - x1; + dy12 = y2 - y1; + + if(y1 == y2) + { + last = y1; + } + else + { + last = y1 - 1; + } + for(y = y0; y <= last; y++) + { + a = x0 + sa / dy01; + b = x0 + sb / dy02; + sa += dx01; + sb += dx02; + if(a > b) + { + _swap(&a, &b); + } + _draw_h_line(a, b, y, color); + } + sa = dx12 * (y - y1); + sb = dx02 * (y - y0); + for(; y <= y2; y++) + { + a = x1 + sa / dy12; + b = x0 + sb / dy02; + sa += dx12; + sb += dx02; + if(a > b) + { + _swap(&a, &b); + } + _draw_h_line(a, b, y, color); + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/***************************************************************************** + * @name :void GUI_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size,u8 mode) + * @date :2018-08-27 + * @function :Display a single English character + * @parameters :x:the bebinning x coordinate of the Character display position + y:the bebinning y coordinate of the Character display position + chr:the ascii code of display character(0~94) + Char_Size:the size of display character(8,16) + mode:0-white background and black character + 1-black background and white character + * @retvalue :None +******************************************************************************/ +void GUI_ShowChar(u8 x, u8 y, u8 chr, u8 Char_Size, u8 mode) +{ + unsigned char c = 0, i = 0, tmp, j = 0; + c = chr - ' '; //得到偏移后的值 + if(x > WIDTH-1) {x = 0; y = y + 2;} + if(Char_Size == 16) + { + /* 处理16位字符 */ + for(i = 0; i < 16; i++) + { + if(mode) + { + tmp = F8X16[c * 16 + i]; + } + else + { + tmp = ~(F8X16[c * 16 + i]); + } + for(j = 0; j < 8; j++) + { + /* 设置点阵中点的值 */ + if(tmp & (0x80 >> j)) + { + SSD1306SetPixel(x + j, y + i, 1); + } + else + { + SSD1306SetPixel(x + j, y + i, 0); + } + } + } + } + else if(Char_Size == 8) + { + /* 处理8位字符 */ + for(i = 0; i < 8; i++) + { + if(mode) + { + tmp = F6x8[c][i]; + } else + { + tmp = ~(F6x8[c][i]); + } + for(j = 0; j < 8; j++) + { + /* 设置点阵中点的值 */ + if(tmp & (0x80 >> j)) + { + SSD1306SetPixel(x + j, y + i, 1); + } + else + { + SSD1306SetPixel(x + j, y + i, 0); + } + } + } + } + else + { + return; + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/***************************************************************************** + * @name :void GUI_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size,u8 mode) + * @date :2018-08-27 + * @function :Display English string + * @parameters :x:the bebinning x coordinate of the English string + y:the bebinning y coordinate of the English string + chr:the start address of the English string + Char_Size:the size of display character + mode:0-white background and black character + 1-black background and white character + * @retvalue :None +******************************************************************************/ +void GUI_ShowString(u8 x, u8 y, u8 *chr, u8 Char_Size, u8 mode) +{ + unsigned char j = 0, csize; + if(Char_Size == 16) + { + csize = Char_Size / 2; + } + else if(Char_Size == 8) + { + csize = Char_Size / 2 + 2; + } + else + { + return; + } + while (chr[j] != '\0') + { + GUI_ShowChar(x, y, chr[j], Char_Size, mode); + x += csize; + if(x > 120) + { + x = 0; + y += Char_Size; + } + j++; + } +} + +/***************************************************************************** + * @name :u32 mypow(u8 m,u8 n) + * @date :2018-08-27 + * @function :get the nth power of m (internal call) + * @parameters :m:the multiplier + n:the power + * @retvalue :the nth power of m +******************************************************************************/ +static u32 mypow(u8 m, u8 n) +{ + u32 result = 1; + while (n--) result *= m; + return result; +} + +/***************************************************************************** + * @name :void GUI_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 Size,u8 mode) + * @date :2018-08-27 + * @function :Display number + * @parameters :x:the bebinning x coordinate of the number + y:the bebinning y coordinate of the number + num:the number(0~4294967295) + len:the length of the display number + Size:the size of display number + mode:0-white background and black character + 1-black background and white character + * @retvalue :None +******************************************************************************/ +void GUI_ShowNum(u8 x, u8 y, u32 num, u8 len, u8 Size, u8 mode) +{ + u8 t, temp; + u8 enshow = 0, csize; + if(Size == 16) + { + csize = Size / 2; + } + else if(Size == 8) + { + csize = Size / 2 + 2; + } + else + { + return; + } + for(t = 0; t < len; t++) + { + temp=(num / mypow(10, len - t - 1)) % 10; + if(enshow == 0 && t < (len - 1)) + { + if(temp == 0) + { + GUI_ShowChar(x + csize * t, y, ' ', Size, mode); + continue; + } else { + enshow = 1; + } + } + GUI_ShowChar(x + csize * t, y, temp + '0', Size, mode); + } +} + +/***************************************************************************** + * @name :void GUI_ShowFont16(u8 x,u8 y,u8 *s,u8 mode) + * @date :2018-08-27 + * @function :Display a single 16x16 Chinese character + * @parameters :x:the bebinning x coordinate of the Chinese character + y:the bebinning y coordinate of the Chinese character + s:the start address of the Chinese character + mode:0-white background and black character + 1-black background and white character + * @retvalue :None +******************************************************************************/ +void GUI_ShowFont16(u8 x, u8 y, u8 *s, u8 mode) +{ + u8 i, j, k, tmp; + u16 num; + num = sizeof(cfont16) / sizeof(typFNT_GB16); + for(i = 0; i < num; i++) + { + if((cfont16[i].Index[0] == *s) && (cfont16[i].Index[1] == *(s + 1))) + { + for(j = 0; j < 32; j++) + { + if(mode) + { + tmp = cfont16[i].Msk[j]; + } + else + { + tmp = ~(cfont16[i].Msk[j]); + } + for(k = 0; k < 8; k++) + { + if(tmp & (0x80 >> k)) + { + OLED_Set_Pixel(x + (j % 2) * 8 + k, y + j / 2, 1); + } + else + { + OLED_Set_Pixel(x + (j % 2) * 8 + k, y + j / 2, 0); + } + } + } + break; + } + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +void GUI_ShowFont16_UTF(u8 x, u8 y, u8 *s, u8 mode) +{ + u8 i, j, k, tmp; + u16 num; + num = sizeof(cfont16_UTF) / sizeof(typFNT_GB16_UTF); + for(i = 0; i < num; i++) + { + if((cfont16_UTF[i].Index[0] == *s) && (cfont16_UTF[i].Index[1] == *(s + 1)) && (cfont16_UTF[i].Index[2] == *(s + 2))) + { + for(j = 0; j < 32; j++) + { + if(mode) + { + tmp = cfont16_UTF[i].Msk[j]; + } + else + { + tmp = ~(cfont16_UTF[i].Msk[j]); + } + for(k = 0; k < 8; k++) + { + if(tmp & (0x80 >> k)) + { + OLED_Set_Pixel(x + (j % 2) * 8 + k, y + j / 2, 1); + } + else + { + OLED_Set_Pixel(x + (j % 2) * 8 + k, y + j / 2, 0); + } + } + } + break; + } + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + + +/***************************************************************************** + * @name :void GUI_ShowFont24(u8 x,u8 y,u8 *s,u8 mode) + * @date :2018-08-27 + * @function :Display a single 24x24 Chinese character + * @parameters :x:the bebinning x coordinate of the Chinese character + y:the bebinning y coordinate of the Chinese character + s:the start address of the Chinese character + mode:0-white background and black character + 1-black background and white character + * @retvalue :None +******************************************************************************/ +void GUI_ShowFont24(u8 x, u8 y, u8 *s, u8 mode) +{ + u8 i, j, k, tmp; + u16 num; + num = sizeof(cfont24) / sizeof(typFNT_GB24); + for(i = 0; i < num; i++) + { + if((cfont24[i].Index[0] == *s) && (cfont24[i].Index[1] == *(s + 1))) + { + for(j = 0; j < 72; j++) + { + if(mode) + { + tmp = cfont24[i].Msk[j]; + } + else + { + tmp = ~(cfont24[i].Msk[j]); + } + for(k = 0; k < 8; k++) + { + if(tmp & (0x80 >> k)) + { + OLED_Set_Pixel(x + (j % 3) * 8 + k, y + j / 3, 1); + } + else + { + OLED_Set_Pixel(x + (j % 3) * 8 + k, y + j / 3, 0); + } + } + } + break; + } + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/***************************************************************************** + * @name :void GUI_ShowFont32(u8 x,u8 y,u8 *s,u8 mode) + * @date :2018-08-27 + * @function :Display a single 32x32 Chinese character + * @parameters :x:the bebinning x coordinate of the Chinese character + y:the bebinning y coordinate of the Chinese character + s:the start address of the Chinese character + mode:0-white background and black character + 1-black background and white character + * @retvalue :None +******************************************************************************/ +void GUI_ShowFont32(u8 x, u8 y, u8 *s, u8 mode) +{ + u8 i, j, k, tmp; + u16 num; + num = sizeof(cfont32) / sizeof(typFNT_GB32); + for(i = 0; i < num; i++) + { + if((cfont32[i].Index[0] == *s) && (cfont32[i].Index[1] == *(s + 1))) + { + for(j = 0; j < 128; j++) + { + if(mode) + { + tmp = cfont32[i].Msk[j]; + } + else + { + tmp = ~(cfont32[i].Msk[j]); + } + for(k = 0; k < 8; k++) + { + if(tmp & (0x80 >> k)) + { + OLED_Set_Pixel(x + (j % 4) * 8 + k, y + j / 4, 1); + } + else + { + OLED_Set_Pixel(x + (j % 4) * 8 + k, y + j / 4, 0); + } + } + } + break; + } + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + +/***************************************************************************** + * @name :void GUI_ShowCHinese(u8 x,u8 y,u8 hsize,u8 *str,u8 mode) + * @date :2018-08-27 + * @function :Display Chinese strings + * @parameters :x:the bebinning x coordinate of the Chinese strings + y:the bebinning y coordinate of the Chinese strings + size:the size of Chinese strings + str:the start address of the Chinese strings + mode:0-white background and black character + 1-black background and white character + * @retvalue :None +******************************************************************************/ +void GUI_ShowChinese(u8 x, u8 y, u8 hsize, u8 *str, u8 mode) +{ + while(*str != '\0') + { + if(hsize == 16) + { + GUI_ShowFont16(x, y, str, mode); + } + else if(hsize == 24) + { + GUI_ShowFont24(x, y, str, mode); + } + else if(hsize == 32) + { + GUI_ShowFont32(x, y, str, mode); + } + else + { + return; + } + x += hsize; + if(x > WIDTH - hsize) + { + x = 0; + y += hsize; + } + str += 2; + } +} + +/***************************************************************************** + * @name :void GUI_DrawBMP(u8 x,u8 y,u8 width, u8 height, u8 BMP[], u8 mode) + * @date :2018-08-27 + * @function :Display a BMP monochromatic picture + * @parameters :x:the bebinning x coordinate of the BMP monochromatic picture + y:the bebinning y coordinate of the BMP monochromatic picture + width:the width of BMP monochromatic picture + height:the height of BMP monochromatic picture + BMP:the start address of BMP monochromatic picture array + mode:0-white background and black character + 1-black background and white character + * @retvalue :None +******************************************************************************/ +void GUI_DrawBMP(u8 x, u8 y, u8 width, u8 height, u8 BMP[], u8 mode) +{ + u8 i, j, k; + u8 tmp; + for(i = 0; i < height; i++) + { + for(j = 0; j < (width + 7) / 8; j++) + { + if(mode) + { + tmp = BMP[i * ((width + 7) / 8) + j]; + } + else + { + tmp = ~BMP[i * ((width + 7) / 8) + j]; + } + for(k = 0; k < 8; k++) + { + if(tmp & (0x80 >> k)) + { + OLED_Set_Pixel(x + j * 8 + k, y + i, 1); + } + else + { + OLED_Set_Pixel(x + j * 8 + k, y + i, 0); + } + } + } + } + //SSD1306Display(); //接口不再刷新,画完后统一刷新 +} + diff --git a/common/ssd1306/ssd1306_gui.h b/common/ssd1306/ssd1306_gui.h new file mode 100644 index 0000000000000000000000000000000000000000..099abd0d57f2f8b0466e46ed622e004e603925e9 --- /dev/null +++ b/common/ssd1306/ssd1306_gui.h @@ -0,0 +1,24 @@ +#ifndef __SSD1306_GUI_H__ +#define __SSD1306_GUI_H__ + +void GUI_DrawPoint(u8 x, u8 y, u8 color); +void GUI_Fill(u8 sx, u8 sy, u8 ex, u8 ey, u8 color); +void GUI_DrawLine(u8 x1, u8 y1, u8 x2, u8 y2, u8 color); +void GUI_DrawRectangle(u8 x1, u8 y1, u8 x2, u8 y2, u8 color); +void GUI_FillRectangle(u8 x1, u8 y1, u8 x2, u8 y2, u8 color); +void GUI_DrawCircle(u8 xc, u8 yc, u8 color, u8 r); +void GUI_FillCircle(u8 xc, u8 yc, u8 color, u8 r); +void GUI_DrawTriangel(u8 x0, u8 y0, u8 x1, u8 y1, u8 x2, u8 y2, u8 color); +void GUI_FillTriangel(u8 x0, u8 y0, u8 x1, u8 y1, u8 x2, u8 y2, u8 color); +void GUI_ShowChar(u8 x, u8 y, u8 chr, u8 Char_Size, u8 mode); +void GUI_ShowNum(u8 x, u8 y, u32 num, u8 len, u8 Size, u8 mode); +void GUI_ShowString(u8 x, u8 y, u8 *chr, u8 Char_Size, u8 mode); +void GUI_ShowFont16(u8 x, u8 y, u8 *s, u8 mode); +void GUI_ShowFont24(u8 x, u8 y, u8 *s, u8 mode); +void GUI_ShowFont32(u8 x, u8 y, u8 *s, u8 mode); +void GUI_ShowChinese(u8 x, u8 y, u8 hsize, u8 *str, u8 mode); +void GUI_DrawBMP(u8 x, u8 y, u8 width, u8 height, u8 BMP[], u8 mode); + + +#endif + diff --git "a/common/ssd1306/ssd1306_iic_drv - \345\211\257\346\234\254.c" "b/common/ssd1306/ssd1306_iic_drv - \345\211\257\346\234\254.c" new file mode 100644 index 0000000000000000000000000000000000000000..4f7dd47cf67531d2a41214e51845a26787c5e8c4 --- /dev/null +++ "b/common/ssd1306/ssd1306_iic_drv - \345\211\257\346\234\254.c" @@ -0,0 +1,307 @@ +#include "ssd1306_iic_drv.h" + +#define SSD1306_I2C_ADDR (0x3C <<1) +#define OLED_IIC_BAUDRATE 400000 + + +//定义I2C和SPI的宏 +#define SSD1306_USE_IIC 1 +#define SSD1306_USE_SPI 0 + + +//OLED显存总共分为8页 +//每页8行,一行128个像素点 +//OLED的显存 +//存放格式如下. +//[0]0 1 2 3 ... 127 (0~7)行 +//[1]0 1 2 3 ... 127 (8~15)行 +//[2]0 1 2 3 ... 127 (16~23)行 +//[3]0 1 2 3 ... 127 (24~31)行 +//[4]0 1 2 3 ... 127 (32~39)行 +//[5]0 1 2 3 ... 127 (40~47)行 +//[6]0 1 2 3 ... 127 (48~55)行 +//[7]0 1 2 3 ... 127 (56~63)行 + +//数组每个bit存储OLED每个像素点的颜色值(1-亮(白色),0-灭(黑色)) +//SSD1306共64*128点阵,每个数组元素为8位,表示1列8个像素点,一共128列, +//因此数组的大小为64/8*128=1024 +static unsigned char OLED_buffer[1024] = {0x00}; + +#if SSD1306_USE_IIC +/******************************************************************* + * @name :void SSD1306InitGPIO(void) + * @date :2018-08-27 + * @function :初始化SSD1306的GPIO + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306InitGPIO(void) +{ + return; +} + +/******************************************************************* + * @name :void SSD1306IOInit(u8 i2cId) + * @date :2023-02-24 + * @function :初始化SSD1306的IIC,在此之前必须先初始化SSD1306的GPIO为IIC功能 + 即在调用本函数之前先调用iot_gpio_iic.h中的初始化接口函数 + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306InitIO(u8 i2cId) +{ + u32 ret = KP_ERR_SUCCESS; + + /* IIC初始化 */ + ret = IoTI2cInit(i2cId, OLED_IIC_BAUDRATE); + if (ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("IoTI2cInit failed :%#x \r\n", ret); + return; + } + + /* IIC设置波特率 */ + ret = IoTI2cSetBaudrate(i2cId, OLED_IIC_BAUDRATE); + if (ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("IoTI2cSetBaudrate failed :%#x \r\n", ret); + return; + } + + printf("SSD1306InitIO success \r\n"); +} + +/******************************************************************* + * @name :void OLED_Reset(void) + * @date :2018-08-27 + * @function :Reset OLED screen + * @parameters :dat:0-Display full black + 1-Display full white + * @retvalue :None +********************************************************************/ +void SSD1306Reset(void) +{ + /* for I2C - do nothing */ +} + +u32 SSD1306WriteData(u32 iicId, u8 *data, u32 byteLen) +{ + u8 buff[WIDTH * 2] = {0}; + if (byteLen <= WIDTH) { + for (u32 i = 0; i < byteLen; i++) { + buff[i*2] = SSD1306_CTRL_DATA | SSD1306_MASK_CONT; + buff[i*2+1] = data[i]; + } + data[(data_len - 1) * 2] = SSD1306_CTRL_DATA; + int ret = IoTI2cWrite(iicId, SSD1306_I2C_ADDR, buff, sizeof(buff)); + //printf("IoTI2cWrite ret = %d\n",ret); + } else { + } +} + +/******************************************************************* + * @name :void OLED_WR_Byte(unsigned dat,unsigned cmd) + * @date :2018-08-27 + * @function :Write a byte of content to the OLED screen + * @parameters :dat:Content to be written + dataType:0-write command + 1-write data + * @retvalue :None +********************************************************************/ +u32 SSD1306WriteByte(u32 spiId, u8 dat, u32 dataType) +{ + u32 ret = KP_ERR_SUCCESS; + + // 先确定数据类型 + if(dataType) { + // 高电平写数据 + ret = SSD1306_DC_Set(); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("SSD1306_DC_Set failed :%#x \r\n", ret); + return ret; + } + } else { + // 低电平写命令 + ret = SSD1306_DC_Clr(); + if(ret != KP_ERR_SUCCESS){ + IOT_PRINTF_FUNC("SSD1306_DC_Clr failed :%#x \r\n", ret); + return ret; + } + } + + // 低电平片选 + ret = SSD1306_CS_Clr(); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("SSD1306_CS_Clr failed :%#x \r\n", ret); + return ret; + } + + // 写数据 + ret = SSD1306WriteData(spiId, dat); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("Ssd1306WriteData failed :%#x \r\n", ret); + return ret; + } + + // 取消片选 + ret = SSD1306_CS_Set(); + if(ret != KP_ERR_SUCCESS) { + IOT_PRINTF_FUNC("SSD1306_CS_Set failed :%#x \r\n", ret); + return ret; + } + + return KP_ERR_SUCCESS; +} +#endif +/******************************************************************* + * @name :void OLED_Init(void) + * @date :2018-08-27 + * @function :initialise OLED SH1106 control IC + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306Init(void) +{ + //SSD1306InitGPIO(); //初始化GPIO + //delay_ms(200); + SSD1306Reset(); //复位OLED + printf("%s,%s,%d \r\n", __FILE__, __func__, __LINE__); + +/**************初始化SSD1306*****************/ + SSD1306WriteByte(0xAE, SSD1306_CMD); /*display off*/ + SSD1306WriteByte(0x00, SSD1306_CMD); /*set lower column address*/ + SSD1306WriteByte(0x10, SSD1306_CMD); /*set higher column address*/ + SSD1306WriteByte(0x40, SSD1306_CMD); /*set display start line*/ + SSD1306WriteByte(0xB0, SSD1306_CMD); /*set page address*/ + SSD1306WriteByte(0x81, SSD1306_CMD); /*contract control*/ + SSD1306WriteByte(0xFF, SSD1306_CMD); /*128*/ + SSD1306WriteByte(0xA1, SSD1306_CMD); /*set segment remap*/ + SSD1306WriteByte(0xA6, SSD1306_CMD); /*normal / reverse*/ + SSD1306WriteByte(0xA8, SSD1306_CMD); /*multiplex ratio*/ + SSD1306WriteByte(0x3F, SSD1306_CMD); /*duty = 1/64*/ + SSD1306WriteByte(0xC8, SSD1306_CMD); /*Com scan direction*/ + SSD1306WriteByte(0xD3, SSD1306_CMD); /*set display offset*/ + SSD1306WriteByte(0x00, SSD1306_CMD); + SSD1306WriteByte(0xD5, SSD1306_CMD); /*set osc division*/ + SSD1306WriteByte(0x80, SSD1306_CMD); + SSD1306WriteByte(0xD9, SSD1306_CMD); /*set pre-charge period*/ + SSD1306WriteByte(0XF1, SSD1306_CMD); + SSD1306WriteByte(0xDA, SSD1306_CMD); /*set COM pins*/ + SSD1306WriteByte(0x12, SSD1306_CMD); + SSD1306WriteByte(0xDB, SSD1306_CMD); /*set vcomh*/ + SSD1306WriteByte(0x30, SSD1306_CMD); + SSD1306WriteByte(0x8D, SSD1306_CMD); /*set charge pump disable*/ + SSD1306WriteByte(0x14, SSD1306_CMD); + SSD1306WriteByte(0xAF, SSD1306_CMD); /*display ON*/ + + printf("%s,%s,%d \r\n", __FILE__, __func__, __LINE__); +} + +/******************************************************************* + * @name :void OLED_Set_Pos(unsigned char x, unsigned char y) + * @date :2018-08-27 + * @function :Set coordinates in the OLED screen + * @parameters :x:x coordinates + y:y coordinates + * @retvalue :None +********************************************************************/ +void SSD1306SetPos(unsigned char x, unsigned char y) +{ + SSD1306WriteByte(YLevel+y/PAGE_SIZE, SSD1306_CMD); + SSD1306WriteByte((((x+2)&0xf0)>>4)|0x10, SSD1306_CMD); + SSD1306WriteByte(((x+2)&0x0f), SSD1306_CMD); +} + +/******************************************************************* + * @name :void OLED_Display_On(void) + * @date :2018-08-27 + * @function :Turn on OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOn(void) +{ + SSD1306WriteByte(0X8D, SSD1306_CMD); //SET DCDC命令 + SSD1306WriteByte(0X14, SSD1306_CMD); //DCDC ON + SSD1306WriteByte(0XAF, SSD1306_CMD); //DISPLAY ON +} + +/******************************************************************* + * @name :void OLED_Display_Off(void) + * @date :2018-08-27 + * @function :Turn off OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOff(void) +{ + SSD1306WriteByte(0X8D, SSD1306_CMD); //SET DCDC命令 + SSD1306WriteByte(0X10, SSD1306_CMD); //DCDC OFF + SSD1306WriteByte(0XAE, SSD1306_CMD); //DISPLAY OFF +} + +/******************************************************************* + * @name :void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color) + * @date :2018-08-27 + * @function :set the value of pixel to RAM + * @parameters :x:the x coordinates of pixel + y:the y coordinates of pixel + color:the color value of the point + 1-white + 0-black + * @retvalue :None +********************************************************************/ +void SSD1306SetPixel(unsigned char x, unsigned char y,unsigned char color) +{ + if(color) + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] |= (1<<(y%PAGE_SIZE))&0xff; + } + else + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] &= ~((1<<(y%PAGE_SIZE))&0xff); + } +} + +/******************************************************************* + * @name :void OLED_Display(void) + * @date :2018-08-27 + * @function :Display in OLED screen + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306Display(void) +{ + u8 i,n; + for(i=0;i>4)|0x10, SSD1306_CMD); + SSD1306WriteByte(((x+2)&0x0f), SSD1306_CMD); +} + +/******************************************************************* + * @name :void OLED_Display_On(void) + * @date :2018-08-27 + * @function :Turn on OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOn(void) +{ + SSD1306WriteByte(0X8D, SSD1306_CMD); //SET DCDC命令 + SSD1306WriteByte(0X14, SSD1306_CMD); //DCDC ON + SSD1306WriteByte(0XAF, SSD1306_CMD); //DISPLAY ON +} + +/******************************************************************* + * @name :void OLED_Display_Off(void) + * @date :2018-08-27 + * @function :Turn off OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOff(void) +{ + SSD1306WriteByte(0X8D, SSD1306_CMD); //SET DCDC命令 + SSD1306WriteByte(0X10, SSD1306_CMD); //DCDC OFF + SSD1306WriteByte(0XAE, SSD1306_CMD); //DISPLAY OFF +} + +/******************************************************************* + * @name :void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color) + * @date :2018-08-27 + * @function :set the value of pixel to RAM + * @parameters :x:the x coordinates of pixel + y:the y coordinates of pixel + color:the color value of the point + 1-white + 0-black + * @retvalue :None +********************************************************************/ +void SSD1306SetPixel(unsigned char x, unsigned char y,unsigned char color) +{ + if(color) + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] |= (1<<(y%PAGE_SIZE))&0xff; + } + else + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] &= ~((1<<(y%PAGE_SIZE))&0xff); + } +} + +/******************************************************************* + * @name :void OLED_Display(void) + * @date :2018-08-27 + * @function :Display in OLED screen + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306Display(void) +{ + u8 i,n; + for(i=0;i>4)|0x10, SSD1306_CMD); + SSD1306WRByte(((x+2)&0x0f), SSD1306_CMD); +} + +/******************************************************************* + * @name :void OLED_Display_On(void) + * @date :2018-08-27 + * @function :Turn on OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOn(void) +{ + SSD1306WRByte(0X8D, SSD1306_CMD); //SET DCDC命令 + SSD1306WRByte(0X14, SSD1306_CMD); //DCDC ON + SSD1306WRByte(0XAF, SSD1306_CMD); //DISPLAY ON +} + +/******************************************************************* + * @name :void OLED_Display_Off(void) + * @date :2018-08-27 + * @function :Turn off OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOff(void) +{ + SSD1306WRByte(0X8D, SSD1306_CMD); //SET DCDC命令 + SSD1306WRByte(0X10, SSD1306_CMD); //DCDC OFF + SSD1306WRByte(0XAE, SSD1306_CMD); //DISPLAY OFF +} + +/******************************************************************* + * @name :void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color) + * @date :2018-08-27 + * @function :set the value of pixel to RAM + * @parameters :x:the x coordinates of pixel + y:the y coordinates of pixel + color:the color value of the point + 1-white + 0-black + * @retvalue :None +********************************************************************/ +void SSD1306SetPixel(unsigned char x, unsigned char y,unsigned char color) +{ + if(color) + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] |= (1<<(y%PAGE_SIZE))&0xff; + } + else + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] &= ~((1<<(y%PAGE_SIZE))&0xff); + } +} + +/******************************************************************* + * @name :void OLED_Display(void) + * @date :2018-08-27 + * @function :Display in OLED screen + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306Display(void) +{ + u8 i,n; + for(i=0;i>4)|0x10); + SSD1306WRCmd(((x+2)&0x0f)); +} + +/******************************************************************* + * @name :void OLED_Display_On(void) + * @date :2018-08-27 + * @function :Turn on OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOn(void) +{ + SSD1306WRCmd(0X8D); //SET DCDC命令 + SSD1306WRCmd(0X14); //DCDC ON + SSD1306WRCmd(0XAF); //DISPLAY ON +} + +/******************************************************************* + * @name :void OLED_Display_Off(void) + * @date :2018-08-27 + * @function :Turn off OLED display + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306DisplayOff(void) +{ + SSD1306WRCmd(0X8D); //SET DCDC命令 + SSD1306WRCmd(0X10); //DCDC OFF + SSD1306WRCmd(0XAE); //DISPLAY OFF +} + +/******************************************************************* + * @name :void OLED_Set_Pixel(unsigned char x, unsigned char y,unsigned char color) + * @date :2018-08-27 + * @function :set the value of pixel to RAM + * @parameters :x:the x coordinates of pixel + y:the y coordinates of pixel + color:the color value of the point + 1-white + 0-black + * @retvalue :None +********************************************************************/ +void SSD1306SetPixel(unsigned char x, unsigned char y,unsigned char color) +{ + if(color) + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] |= (1<<(y%PAGE_SIZE))&0xff; + } + else + { + OLED_buffer[(y/PAGE_SIZE)*WIDTH+x] &= ~((1<<(y%PAGE_SIZE))&0xff); + } +} + +/******************************************************************* + * @name :void OLED_Display(void) + * @date :2018-08-27 + * @function :Display in OLED screen + * @parameters :None + * @retvalue :None +********************************************************************/ +void SSD1306Display(void) +{ + u8 i,n; + for(i=0;i +#include + +/* 基本数据类型 */ +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; +typedef unsigned long ulong; +typedef char s8; +typedef short s16; +typedef int s32; +typedef long long s64; +typedef long slong; + +#undef ERROR +#define ERROR (-1) + +#define KP_ERR_SUCCESS 0 +#define KP_ERR_FAILURE (-1) + +#ifndef NULL +#define NULL ((void *)0) +#endif + +#define IOT_U8_MAX 0xFF +#define IOT_U16_MAX 0xFFFF +#define IOT_U32_MAX 0xFFFFFFFF +#define IOT_U64_MAX 0xFFFFFFFFFFFFFFFFUL + +#define msleep(x) usleep((x)*1000) + +typedef enum { + GPIO_OUT_LOW = 0, /* 低电平 */ + GPIO_OUT_HIGH = 1 /* 高电平 */ +} gpio_out_value; + +typedef enum { + /** Physical port 0 */ + IOT_UART_IDX_0, + /** Physical port 1 */ + IOT_UART_IDX_1, + /** Physical port 2 */ + IOT_UART_IDX_2, + /** Maximum value */ + IOT_UART_IDX_MAX +}uart_port_idx; + + +typedef enum { + PWM_PORT_PWM0 = 0, /* PWM0端口 */ + PWM_PORT_PWM1 = 1, /* PWM1端口 */ + PWM_PORT_PWM2 = 2, /* PWM2端口 */ + PWM_PORT_PWM3 = 3, /* PWM3端口 */ + PWM_PORT_PWM4 = 4, /* PWM4端口 */ + PWM_PORT_PWM5 = 5, /* PWM5端口 */ + PWM_PORT_MAX /* 最大值,不可使用 */ +} pwm_port; + +typedef enum { + PWM_OUT_STOP = 0, /* PWM停止 */ + PWM_OUT_START = 1 /* PWM启动 */ +} pwm_out_value; + +typedef enum { + /** I2C hardware index 0 */ + IOT_I2C_IDX_0, + /** I2C hardware index 1 */ + IOT_I2C_IDX_1, +} i2c_port_idx; + + +typedef enum { + LED_OFF = 0, + LED_ON = 1, + LED_SPARK = 2 +} led_state; +#define LED_SPARK_INTERVAL_TIME 30 + +#define IOT_DEBUG(fmt, args...) do { \ + printf("[IOT][%s][%d]", __FILE__, __LINE__); \ + printf(fmt, ##args); \ + printf("\r\n"); \ + } while (0) + +#define IOT_PRINTF_FUNC(fmt, args...) do { \ + printf("[%s]", __FUNCTION__); \ + printf(fmt, ##args); \ + printf("\r\n"); \ + } while (0) + + + +#endif diff --git a/include/wifiiot_errno.h b/include/wifiiot_errno.h new file mode 100644 index 0000000000000000000000000000000000000000..b32808f4d37279a05a7e4208e2718561ee1e6d53 --- /dev/null +++ b/include/wifiiot_errno.h @@ -0,0 +1,536 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup wifiiot + * @{ + * + * @brief Provides dedicated device operation interfaces on the Wi-Fi module, + * including ADC, AT, flash, GPIO, I2C, I2S, partition, PWM, SDIO, UART, and watchdog. + * + * + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file wifiiot_errno.h + * + * @brief Defines error codes used by the Wi-Fi module. + * + * @since 1.0 + * @version 1.0 + */ + +#ifndef WIFIIOT_ERRNO_H +#define WIFIIOT_ERRNO_H + +/** + * @brief Defines a module-level return value to indicate a successful operation. + * + */ +#define WIFI_IOT_SUCCESS 0 +/** + * @brief Defines a module-level return value to indicate an operation failure. + * + */ +#define WIFI_IOT_FAILURE (-1) + +/** + * @brief Defines a UART error code to indicate an invalid parameter. + * + */ +#define WIFIIOT_ERR_UART_INVALID_PARAMETER 0x80001000 +/** + * @brief Defines a UART error code to indicate an invalid pause operation. + * + */ +#define WIFI_IOT_ERR_UART_INVALID_SUSPEND 0x80001001 +/** + * @brief Defines a UART error code to indicate an invalid parity check. + * + */ +#define WIFI_IOT_ERR_UART_INVALID_PARITY 0x80001002 +/** + * @brief Defines a UART error code to indicate an invalid data bit. + * + */ +#define WIFI_IOT_ERR_UART_INVALID_DATA_BITS 0x80001003 +/** + * @brief Defines a UART error code to indicate an invalid stop bit. + * + */ +#define WIFI_IOT_ERR_UART_INVALID_STOP_BITS 0x80001004 +/** + * @brief Defines a UART error code to indicate an invalid baud rate. + * + */ +#define WIFI_IOT_ERR_UART_INVALID_BAUD 0x80001005 +/** + * @brief Defines a UART error code to indicate an invalid port number. + * + */ +#define WIFI_IOT_ERR_UART_INVALID_COM_PORT 0x80001006 +/** + * @brief Defines a UART error code to indicate a non-support for DMA. + * + */ +#define WIFI_IOT_ERR_UART_NOT_SUPPORT_DMA 0x80001007 +/** + * @brief Defines a UART error code to indicate the non-block mode. + * + */ +#define WIFI_IOT_ERR_UART_NOT_BLOCK_MODE 0x80001008 + + +/** + * @brief Defines a GPIO error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_GPIO_INVALID_PARAMETER 0x80001040 +/** + * @brief Defines a GPIO error code to indicate repeated initialization. + * + */ +#define WIFI_IOT_ERR_GPIO_REPEAT_INIT 0x80001041 +/** + * @brief Defines a GPIO error code to indicate non-initialization. + * + */ +#define WIFI_IOT_ERR_GPIO_NOT_INIT 0x80001042 +/** + * @brief Defines a GPIO error code to indicate a non-support. + * + */ +#define WIFI_IOT_ERR_GPIO_NOT_SUPPORT 0x80001043 + +/** + * @brief Defines a flash error code to indicate non-initialization. + * + */ +#define WIFI_IOT_ERR_FLASH_NOT_INIT 0x800010C0 +/** + * @brief Defines a flash error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM 0x800010C1 +/** + * @brief Defines a flash error code to indicate that the address is out of range. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_BEYOND_ADDR 0x800010C2 +/** + * @brief Defines a flash error code to indicate that the parameter size is 0 + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_SIZE_ZERO 0x800010C3 +/** + * @brief Defines a flash error code to indicate that the erase size is not aligned. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_ERASE_NOT_ALIGN 0x800010C4 +/** + * @brief Defines a flash error code to indicate that the data of the I/O controller is empty. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_IOCTRL_DATA_NULL 0x800010C5 +/** + * @brief Defines a flash error code to indicate empty data. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_DATA_NULL 0x800010C6 +/** + * @brief Defines a flash error code to indicate that pad1 is incorrect. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_PAD1 0x800010C7 +/** + * @brief Defines a flash error code to indicate that pad2 is incorrect. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_PAD2 0x800010C8 +/** + * @brief Defines a flash error code to indicate that pad3 is incorrect. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_PAD3 0x800010C9 +/** + * @brief Defines a flash error code to indicate that pad4 is incorrect. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_PARAM_PAD4 0x800010CA +/** + * @brief Defines a flash error code to indicate a timeout on waiting for ready. + * + */ +#define WIFI_IOT_ERR_FLASH_TIME_OUT_WAIT_READY 0x800010CB +/** + * @brief Defines a flash error code to indicate that an error occurs when reading register 1. + * + */ +#define WIFI_IOT_ERR_FLASH_QUAD_MODE_READ_REG1 0x800010CC +/** + * @brief Defines a flash error code to indicate that an error occurs when reading register 2. + * + */ +#define WIFI_IOT_ERR_FLASH_QUAD_MODE_READ_REG2 0x800010CD +/** + * @brief Defines a flash error code to indicate that an error occurs when comparing registers. + * + */ +#define WIFI_IOT_ERR_FLASH_QUAD_MODE_COMPARE_REG 0x800010CE +/** + * @brief Defines a flash error code to indicate a flash mismatch + * + */ +#define WIFI_IOT_ERR_FLASH_NO_MATCH_FLASH 0x800010CF +/** + * @brief Defines a flash error code to indicate a failure in enabling write. + * + */ +#define WIFI_IOT_ERR_FLASH_WRITE_ENABLE 0x800010D0 +/** + * @brief Defines a flash error code to indicate a mismatch in the size of data to erase. + * + */ +#define WIFI_IOT_ERR_FLASH_NO_MATCH_ERASE_SIZE 0x800010D1 +/** + * @brief Defines a flash error code to indicate the maximum value of the SPI operation. + * + */ +#define WIFI_IOT_ERR_FLASH_MAX_SPI_OP 0x800010D2 +/** + * @brief Defines a flash error code to indicate an unsupported IO controller ID.. + * + */ +#define WIFI_IOT_ERR_FLASH_NOT_SUPPORT_IOCTRL_ID 0x800010D3 +/** + * @brief Defines a flash error code to indicate an invalid chip ID. + * + */ +#define WIFI_IOT_ERR_FLASH_INVALID_CHIP_ID 0x800010D4 +/** + * @brief Defines a flash error code to indicate repeated initialization. + * + */ +#define WIFI_IOT_ERR_FLASH_RE_INIT 0x800010D5 +/** + * @brief Defines a flash error code to indicate a non-support for data erasure. + * + */ +#define WIFI_IOT_ERR_FLASH_WRITE_NOT_SUPPORT_ERASE 0x800010D6 +/** + * @brief Defines a flash error code to indicate that an error occurs when comparing the data written. + * + */ +#define WIFI_IOT_ERR_FLASH_WRITE_COMPARE_WRONG 0x800010D7 +/** + * @brief Defines a flash error code to indicate a timeout on waiting for a configuration to start. + * + */ +#define WIFI_IOT_ERR_FLASH_WAIT_CFG_START_TIME_OUT 0x800010D8 +/** + * @brief Defines a flash error code to indicate a partition initialization failure. + * + */ +#define WIFI_IOT_ERR_FLASH_PATITION_INIT_FAIL 0x800010D9 +/** + * @brief Defines a flash error code to indicate initialization. + * + */ +#define WIFI_IOT_ERR_FLASH_INITILIZATION 0x800010DA +/** + * @brief Defines a flash error code to indicate that the size of data to erase is not a multiple of 4K bytes. + * + */ +#define WIFI_IOT_ERR_FLASH_ERASE_NOT_4K_ALIGN 0x800010DB +/** + * @brief Defines a flash error code to indicate a non-support. + * + */ +#define WIFI_IOT_ERR_FLASH_PROTECT_NOT_SUPPORT 0x800010DC +/** + * @brief Defines a flash error code to indicate non-initialization. + * + */ +#define WIFI_IOT_ERR_FLASH_PROTECT_NOT_INIT 0x800010DD +/** + * @brief Defines a flash error code to indicate repeated initialization. + * + */ +#define WIFI_IOT_ERR_FLASH_PROTECT_RE_INIT 0x800010DE +/** + * @brief Defines a flash error code to indicate that no chip is found. + * + */ +#define WIFI_IOT_ERR_FLASH_PROTECT_NOT_FIND_CHIP 0x800010DF +/** + * @brief Defines a flash error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_INVALID_PARAM 0x800010F0 +/** + * @brief Defines a flash error code to indicate that the address is out of range. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_BEYOND_ADDR_SIZE 0x800010F1 +/** + * @brief Defines a flash error code to indicate a failure in applying for memory. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_MALLOC_FAIL 0x800001F2 +/** + * @brief Defines a flash error code to indicate an encryption error. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_DATA_ENCRYPT_ERR 0x800001F3 +/** + * @brief Defines a flash error code to indicate a decryption error. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_DATA_DECRYPT_ERR 0x800001F4 +/** + * @brief Defines a flash error code to indicate an empty key value. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_KEY_EMPTY_ERR 0x800001F5 +/** + * @brief Defines a flash error code to indicate a copy failure. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_MEMCPY_FAIL 0x800001F6 +/** + * @brief Defines a flash error code to indicate a non-support for encryption. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_NOT_SUPPORT 0x800001F7 +/** + * @brief Defines a flash error code to indicate an incorrect parameter during encryption. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_PREPARE_ERR 0x800001F8 +/** + * @brief Defines a flash error code to indicate an invalid key. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_KEY_INVALID_ERR 0x800001F9 +/** + * @brief Defines a flash error code to indicate a failure in saving the key. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_KEY_SAVE_ERR 0x800001FA +/** + * @brief Defines a flash error code to indicate an incorrect kernel address.. + * + */ +#define WIFI_IOT_ERR_FLASH_CRYPTO_KERNEL_ADDR_ERR 0x800001FB + +/** + * @brief Defines an I2C error code to indicate a non-support. + * + */ +#define WIFI_IOT_ERR_I2C_NOT_INIT 0x80001180 +/** + * @brief Defines an I2C error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_I2C_INVALID_PARAMETER 0x80001181 +/** + * @brief Defines an I2C error code to indicate a start timeout. + * + */ +#define WIFI_IOT_ERR_I2C_TIMEOUT_START 0x80001182 +/** + * @brief Defines an I2C error code to indicate a wait timeout. + * + */ +#define WIFI_IOT_ERR_I2C_TIMEOUT_WAIT 0x80001183 +/** + * @brief Defines an I2C error code to indicate a stop timeout. + * + */ +#define WIFI_IOT_ERR_I2C_TIMEOUT_STOP 0x80001184 +/** + * @brief Defines an I2C error code to indicate a receive timeout. + * + */ +#define WIFI_IOT_ERR_I2C_TIMEOUT_RCV_BYTE 0x80001185 +/** + * @brief Defines an I2C error code to indicate a processing timeout. + * + */ +#define WIFI_IOT_ERR_I2C_TIMEOUT_RCV_BYTE_PROC 0x80001186 +/** + * @brief Defines an I2C error code to indicate a waiting failure. + * + */ +#define WIFI_IOT_ERR_I2C_WAIT_SEM_FAIL 0x80001187 +/** + * @brief Defines an I2C error code to indicate a responding failure. + * + */ +#define WIFI_IOT_ERR_I2C_START_ACK_ERR 0x80001188 +/** + * @brief Defines an I2C error code to indicate a failure in waiting for a response. + * + */ +#define WIFI_IOT_ERR_I2C_WAIT_ACK_ERR 0x80001189 + +/** + * @brief Defines an SPI error code to indicate non-initialization. + * + */ +#define WIFI_IOT_ERR_SPI_NOT_INIT 0x800011C0 +/** + * @brief Defines an SPI error code to indicate repeated initialization. + * + */ +#define WIFI_IOT_ERR_SPI_REINIT 0x800011C1 +/** + * @brief Defines an SPI error code to indicate a parameter error. + * + */ +#define WIFI_IOT_ERR_SPI_PARAMETER_WRONG 0x800011C2 +/** + * @brief Defines an SPI error code to indicate the busy state. + * + */ +#define WIFI_IOT_ERR_SPI_BUSY 0x800011C3 +/** + * @brief Defines an SPI error code to indicate a write timeout. + * + */ +#define WIFI_IOT_ERR_SPI_WRITE_TIMEOUT 0x800011C4 +/** + * @brief Defines an SPI error code to indicate a read timeout. + * + */ +#define WIFI_IOT_ERR_SPI_READ_TIMEOUT 0x800011C5 +/** + * @brief Defines an SPI error code to indicate a non-support for DMA. + * + */ +#define WIFI_IOT_ERR_SPI_NOT_SUPPORT_DMA 0x800011C6 + +/** + * @brief Defines an SDIO error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_SDIO_INVALID_PARAMETER 0x80001280 + + +/** + * @brief Defines an ADC error code to indicate a parameter error. + * + */ +#define WIFI_IOT_ERR_ADC_PARAMETER_WRONG 0x80001300 +/** + * @brief Defines an ADC error code to indicate an invalid channel. + * + */ +#define WIFI_IOT_ERR_ADC_INVALID_CHANNEL_ID 0x80001301 +/** + * @brief Defines an ADC error code to indicate a timeout. + * + */ +#define WIFI_IOT_ERR_ADC_TIMEOUT 0x80001302 +/** + * @brief Defines an ADC error code to indicate non-initialization. + * + */ +#define WIFI_IOT_ERR_ADC_NOT_INIT 0x80001303 + +/** + * @brief Defines a PWM error code to indicate non-initialization. + * + */ +#define WIFI_IOT_ERR_PWM_NO_INIT 0x80001340 +/** + * @brief Defines a PWM error code to indicate an initialization error. + * + */ +#define WIFI_IOT_ERR_PWM_INITILIZATION_ALREADY 0x80001341 +/** + * @brief Defines a PWM error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_PWM_INVALID_PARAMETER 0x80001342 + +/** + * @brief Defines a DMA error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_DMA_INVALID_PARA 0x80001380 +/** + * @brief Defines a DMA error code to indicate non-initialization. + * + */ +#define WIFI_IOT_ERR_DMA_NOT_INIT 0x80001381 +/** + * @brief Defines a DMA error code to indicate the busy state. + * + */ +#define WIFI_IOT_ERR_DMA_BUSY 0x80001382 +/** + * @brief Defines a DMA error code to indicate a transmission failure. + * + */ +#define WIFI_IOT_ERR_DMA_TRANSFER_FAIL 0x80001383 +/** + * @brief Defines a DMA error code to indicate a transmission timeout. + * + */ +#define WIFI_IOT_ERR_DMA_TRANSFER_TIMEOUT 0x80001384 +/** + * @brief Defines a DMA error code to indicate a retrieval failure. + * + */ +#define WIFI_IOT_ERR_DMA_GET_NOTE_FAIL 0x80001385 +/** + * @brief Defines a DMA error code to indicate that the LLI is not created. + * + */ +#define WIFI_IOT_ERR_DMA_LLI_NOT_CREATE 0x80001386 +/** + * @brief Defines a DMA error code to indicate a failure in enabling channel interrupt. + * + */ +#define WIFI_IOT_ERR_DMA_CH_IRQ_ENABLE_FAIL 0x80001387 + +/** + * @brief Defines an I2S error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_I2S_INVALID_PARAMETER 0x80001400 +/** + * @brief Defines an I2S error code to indicate a write timeout. + * + */ +#define WIFI_IOT_ERR_I2S_WRITE_TIMEOUT 0x80001401 + +/** + * @brief Defines an AT error code to indicate repeated function registration. + * + */ +#define WIFI_IOT_ERR_AT_NAME_OR_FUNC_REPEAT_REGISTERED 0x80003280 +/** + * @brief Defines an AT error code to indicate an invalid parameter. + * + */ +#define WIFI_IOT_ERR_AT_INVALID_PARAMETER 0x80003281 + +#endif +/** @} */ diff --git a/include/wifiiot_gpio.h b/include/wifiiot_gpio.h new file mode 100644 index 0000000000000000000000000000000000000000..1338f9283a43249b8d3cd90055850ce3d5c036c8 --- /dev/null +++ b/include/wifiiot_gpio.h @@ -0,0 +1,268 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup wifiiot + * @{ + * + * @brief Provides dedicated device operation interfaces on the Wi-Fi module, + * including ADC, AT, flash, GPIO, I2C, I2S, partition, PWM, SDIO, UART, and watchdog. + * + * + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file wifiiot_gpio.h + * + * @brief Declares the GPIO interface functions. + * + * These functions are used for GPIO initialization, + * input/output settings, and level settings. \n + * + * @since 1.0 + * @version 1.0 + */ +#ifndef WIFIIOT_GPIO_H +#define WIFIIOT_GPIO_H + +/** + * @brief Enumerates GPIO pin IDs. + */ +typedef enum { + /** GPIO0 */ + WIFI_IOT_GPIO_IDX_0, + /** GPIO1 */ + WIFI_IOT_GPIO_IDX_1, + /** GPIO2 */ + WIFI_IOT_GPIO_IDX_2, + /** GPIO3 */ + WIFI_IOT_GPIO_IDX_3, + /** GPIO4 */ + WIFI_IOT_GPIO_IDX_4, + /** GPIO5 */ + WIFI_IOT_GPIO_IDX_5, + /** GPIO6 */ + WIFI_IOT_GPIO_IDX_6, + /** GPIO7 */ + WIFI_IOT_GPIO_IDX_7, + /** GPIO8 */ + WIFI_IOT_GPIO_IDX_8, + /** GPIO9 */ + WIFI_IOT_GPIO_IDX_9, + /** GPIO10 */ + WIFI_IOT_GPIO_IDX_10, + /** GPIO11 */ + WIFI_IOT_GPIO_IDX_11, + /** GPIO12 */ + WIFI_IOT_GPIO_IDX_12, + /** GPIO13 */ + WIFI_IOT_GPIO_IDX_13, + /** GPIO14 */ + WIFI_IOT_GPIO_IDX_14, + /** Maximum value */ + WIFI_IOT_GPIO_IDX_MAX, +} WifiIotGpioIdx; + + +/** + * @brief Enumerates GPIO level values. + */ +typedef enum { + /** Low GPIO level */ + WIFI_IOT_GPIO_VALUE0 = 0, + /** High GPIO level */ + WIFI_IOT_GPIO_VALUE1 +} WifiIotGpioValue; + +/** + * @brief Enumerates GPIO directions. + */ +typedef enum { + /** Input */ + WIFI_IOT_GPIO_DIR_IN = 0, + /** Output */ + WIFI_IOT_GPIO_DIR_OUT +} WifiIotGpioDir; + +/** + * @brief Enumerates GPIO interrupt trigger modes. + */ +typedef enum { + /** Level-sensitive interrupt */ + WIFI_IOT_INT_TYPE_LEVEL = 0, + /** Edge-sensitive interrupt */ + WIFI_IOT_INT_TYPE_EDGE +} WifiIotGpioIntType; + +/** + * @brief Enumerates I/O interrupt polarities. + */ +typedef enum { + /** Interrupt at a low level or falling edge */ + WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW = 0, + /** Interrupt at a high level or rising edge */ + WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH +} WifiIotGpioIntPolarity; + +/** + * @brief Indicates the GPIO interrupt callback. + * + */ +typedef void (*GpioIsrCallbackFunc) (char *arg); + +/** + * @brief Initializes the GPIO device. + * + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioInit(void); + +/** + * @brief Deinitializes the GPIO device. + * + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioDeinit(void); + +/** + * @brief Sets the direction for a GPIO pin. + * + * @param id Indicates the GPIO pin ID. + * @param dir Indicates the GPIO input/output direction. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioSetDir(WifiIotGpioIdx id, WifiIotGpioDir dir); + +/** + * @brief Obtains the direction for a GPIO pin. + * + * @param id Indicates the GPIO pin ID. + * @param dir Indicates the pointer to the GPIO input/output direction. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioGetDir(WifiIotGpioIdx id, WifiIotGpioDir *dir); + +/** + * @brief Sets the output level value for a GPIO pin. + * + * @param id Indicates the GPIO pin ID. + * @param val Indicates the output level value. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioSetOutputVal(WifiIotGpioIdx id, WifiIotGpioValue val); + +/** + * @brief Obtains the output level value of a GPIO pin. + * + * @param id Indicates the GPIO pin ID. + * @param val Indicates the pointer to the output level value. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioGetOutputVal(WifiIotGpioIdx id, WifiIotGpioValue *val); + +/** + * @brief Obtains the input level value of a GPIO pin. + * + * @param id Indicates the GPIO pin ID. + * @param val Indicates the pointer to the input level value. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioGetInputVal(WifiIotGpioIdx id, WifiIotGpioValue *val); + +/** + * @brief Enables the interrupt function for a GPIO pin. + * + * This function can be used to set the interrupt type, interrupt polarity, + * and interrupt callback for a GPIO pin. + * + * @param id Indicates the GPIO pin ID. + * @param intType Indicates the interrupt type. + * @param intPolarity Indicates the interrupt polarity. + * @param func Indicates the interrupt callback function. + * @param arg Indicates the pointer to the argument used in the interrupt callback function. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioRegisterIsrFunc(WifiIotGpioIdx id, WifiIotGpioIntType intType, WifiIotGpioIntPolarity intPolarity, + GpioIsrCallbackFunc func, char *arg); + +/** + * @brief Disables the interrupt function for a GPIO pin. + * + * @param id Indicates the GPIO pin ID. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioUnregisterIsrFunc(WifiIotGpioIdx id); + +/** + * @brief Masks the interrupt function for a GPIO pin. + * + * @param id Indicates the GPIO pin ID. + * @param mask Indicates whether the interrupt function is masked. + * The value 1 means to mask the interrupt function, + * and 0 means not to mask the interrupt function. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioSetIsrMask(WifiIotGpioIdx id, unsigned char mask); + +/** + * @brief Sets the interrupt trigger mode of a GPIO pin. + * + * This function configures a GPIO pin based on the interrupt type and interrupt polarity. + * + * @param id Indicates the GPIO pin ID. + * @param intType Indicates the interrupt type. + * @param intPolarity Indicates the interrupt polarity. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int GpioSetIsrMode(WifiIotGpioIdx id, WifiIotGpioIntType intType, WifiIotGpioIntPolarity intPolarity); + +#endif +/** @} */ diff --git a/include/wifiiot_gpio_ex.h b/include/wifiiot_gpio_ex.h new file mode 100644 index 0000000000000000000000000000000000000000..cf32ecc1d91787d8e46fb09479069bfaa88de147 --- /dev/null +++ b/include/wifiiot_gpio_ex.h @@ -0,0 +1,592 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup wifiiot + * @{ + * + * @brief Provides dedicated device operation interfaces on the Wi-Fi module, + * including ADC, AT, flash, GPIO, I2C, I2S, partition, PWM, SDIO, UART, and watchdog. + * + * + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file wifiiot_gpio_ex.h + * + * @brief Declares the extended GPIO interface functions. + * + * These functions are used for settings GPIO pulls and driver strength. \n + * + * @since 1.0 + * @version 1.0 + */ + +#ifndef WIFIIOT_GPIO_EX_H +#define WIFIIOT_GPIO_EX_H + +/** + * @brief Enumerates GPIO hardware pin IDs. + */ +typedef enum { + /** GPIO hardware pin 0 */ + WIFI_IOT_IO_NAME_GPIO_0, + /** GPIO hardware pin 1 */ + WIFI_IOT_IO_NAME_GPIO_1, + /** GPIO hardware pin 2 */ + WIFI_IOT_IO_NAME_GPIO_2, + /** GPIO hardware pin 3 */ + WIFI_IOT_IO_NAME_GPIO_3, + /** GPIO hardware pin 4 */ + WIFI_IOT_IO_NAME_GPIO_4, + /** GPIO hardware pin 5 */ + WIFI_IOT_IO_NAME_GPIO_5, + /** GPIO hardware pin 6 */ + WIFI_IOT_IO_NAME_GPIO_6, + /** GPIO hardware pin 7 */ + WIFI_IOT_IO_NAME_GPIO_7, + /** GPIO hardware pin 8 */ + WIFI_IOT_IO_NAME_GPIO_8, + /** GPIO hardware pin 9 */ + WIFI_IOT_IO_NAME_GPIO_9, + /** GPIO hardware pin 10 */ + WIFI_IOT_IO_NAME_GPIO_10, + /** GPIO hardware pin 11 */ + WIFI_IOT_IO_NAME_GPIO_11, + /** GPIO hardware pin 12 */ + WIFI_IOT_IO_NAME_GPIO_12, + /** GPIO hardware pin 13 */ + WIFI_IOT_IO_NAME_GPIO_13, + /** GPIO hardware pin 14 */ + WIFI_IOT_IO_NAME_GPIO_14, + /** SFC chip select (CS) pin */ + WIFI_IOT_IO_NAME_SFC_CSN, + /** SFC interface pin 1 */ + WIFI_IOT_IO_NAME_SFC_IO1, + /** SFC interface pin 2 */ + WIFI_IOT_IO_NAME_SFC_IO2, + /** SFC interface pin 0 */ + WIFI_IOT_IO_NAME_SFC_IO0, + /** SFC clock pin */ + WIFI_IOT_IO_NAME_SFC_CLK, + /** SFC interface pin 3 */ + WIFI_IOT_IO_NAME_SFC_IO3, + /** Maximum value */ + WIFI_IOT_IO_NAME_MAX, +} WifiIotIoName; + +/** + * @brief Enumerates the functions of GPIO hardware pin 0. + */ +typedef enum { + /** GPIO0 function */ + WIFI_IOT_IO_FUNC_GPIO_0_GPIO, + /** Functions of UART1 TXD */ + WIFI_IOT_IO_FUNC_GPIO_0_UART1_TXD = 2, + /** SPI1 CK function */ + WIFI_IOT_IO_FUNC_GPIO_0_SPI1_CK, + /** Functions of JTAG TD0 */ + WIFI_IOT_IO_FUNC_GPIO_0_JTAG_TDO, + /** PWM3 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_0_PWM3_OUT, + /** I2C1 SDA function */ + WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA, +} WifiIotIoFuncGpio0; + +/** + * @brief Enumerates the functions of GPIO hardware pin 1. + */ +typedef enum { + /** GPIO1 function */ + WIFI_IOT_IO_FUNC_GPIO_1_GPIO, + /** UART1 RXD function */ + WIFI_IOT_IO_FUNC_GPIO_1_UART1_RXD = 2, + /** SPI1 RXD function */ + WIFI_IOT_IO_FUNC_GPIO_1_SPI1_RXD, + /** JTAG TCK function */ + WIFI_IOT_IO_FUNC_GPIO_1_JTAG_TCK, + /** PWM4 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_1_PWM4_OUT, + /** I2C1 SCL function */ + WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL, + /** BT FREQ function */ + WIFI_IOT_IO_FUNC_GPIO_1_BT_FREQ, +} WifiiIotIoFuncGpio1; + +/** + * @brief Enumerates the functions of GPIO hardware pin 2. + */ +typedef enum { + /** GPIO2 function */ + WIFI_IOT_IO_FUNC_GPIO_2_GPIO, + /** UART1 RTS function */ + WIFI_IOT_IO_FUNC_GPIO_2_UART1_RTS_N = 2, + /** SPI1 TXD function */ + WIFI_IOT_IO_FUNC_GPIO_2_SPI1_TXD, + /** JTAG TRSTN function */ + WIFI_IOT_IO_FUNC_GPIO_2_JTAG_TRSTN, + /** PWM2 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_2_PWM2_OUT, + /** SSI CLK function */ + WIFI_IOT_IO_FUNC_GPIO_2_SSI_CLK = 7, +} WifiIotIoFuncGpio2; + +/** + * @brief Enumerates the functions of GPIO hardware pin 3. + */ +typedef enum { + /** GPIO3 function */ + WIFI_IOT_IO_FUNC_GPIO_3_GPIO, + /** UART0 TXD function */ + WIFI_IOT_IO_FUNC_GPIO_3_UART0_TXD, + /** UART1 CTS function */ + WIFI_IOT_IO_FUNC_GPIO_3_UART1_CTS_N, + /** SPI CSN function */ + WIFI_IOT_IO_FUNC_GPIO_3_SPI1_CSN, + /** JTAG TDI function */ + WIFI_IOT_IO_FUNC_GPIO_3_JTAG_TDI, + /** PWM5 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_3_PWM5_OUT, + /** I2C1 SDA function */ + WIFI_IOT_IO_FUNC_GPIO_3_I2C1_SDA, + /** SSI DATA function */ + WIFI_IOT_IO_FUNC_GPIO_3_SSI_DATA, +} WifiIotIoFuncGpio3; + +/** + * @brief Enumerates the functions of GPIO hardware pin 4. + */ +typedef enum { + /** GPIO4 function */ + WIFI_IOT_IO_FUNC_GPIO_4_GPIO, + /** UART0 RXD function */ + WIFI_IOT_IO_FUNC_GPIO_4_UART0_RXD = 2, + /** JTAG TMS function */ + WIFI_IOT_IO_FUNC_GPIO_4_JTAG_TMS = 4, + /** PWM1 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_4_PWM1_OUT, + /** I2C1 SCL function */ + WIFI_IOT_IO_FUNC_GPIO_4_I2C1_SCL, +} WifiIotIoFuncGpio4; + +/** + * @brief Enumerates the functions of GPIO hardware pin 5. + */ +typedef enum { + /** GPIO5 function */ + WIFI_IOT_IO_FUNC_GPIO_5_GPIO, + /** UART1 RXD function */ + WIFI_IOT_IO_FUNC_GPIO_5_UART1_RXD = 2, + /** SPI0 CSN function */ + WIFI_IOT_IO_FUNC_GPIO_5_SPI0_CSN, + /** PWM2 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_5_PWM2_OUT = 5, + /** I2C0 MCLK function */ + WIFI_IOT_IO_FUNC_GPIO_5_I2S0_MCLK, + /** BT STATUS function */ + WIFI_IOT_IO_FUNC_GPIO_5_BT_STATUS, +} WifiIotIoFuncGpio5; + +/** + * @brief Enumerates the functions of GPIO hardware pin 6. + */ +typedef enum { + /** GPIO6 function */ + WIFI_IOT_IO_FUNC_GPIO_6_GPIO, + /** UART1 TXD function */ + WIFI_IOT_IO_FUNC_GPIO_6_UART1_TXD = 2, + /** SPI0 CK function */ + WIFI_IOT_IO_FUNC_GPIO_6_SPI0_CK, + /** PWM3 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_6_PWM3_OUT = 5, + /** I2S0 TX function */ + WIFI_IOT_IO_FUNC_GPIO_6_I2S0_TX, + /** COEX switch function */ + WIFI_IOT_IO_FUNC_GPIO_6_COEX_SWITCH, +} WifiIotIoFuncGpio6; + +/** + * @brief Enumerates the functions of GPIO hardware pin 7. + */ +typedef enum { + /** GPIO7 function */ + WIFI_IOT_IO_FUNC_GPIO_7_GPIO, + /** UART1 CTS function */ + WIFI_IOT_IO_FUNC_GPIO_7_UART1_CTS_N = 2, + /** SPI0 RXD function */ + WIFI_IOT_IO_FUNC_GPIO_7_SPI0_RXD, + /** PWM0 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_7_PWM0_OUT = 5, + /** I2S0 BCLK function */ + WIFI_IOT_IO_FUNC_GPIO_7_I2S0_BCLK, + /** BT ACTIVE function */ + WIFI_IOT_IO_FUNC_GPIO_7_BT_ACTIVE, +} WifiIotIoFuncGpio7; + +/** + * @brief Enumerates the functions of GPIO hardware pin 8. + */ +typedef enum { + /** GPIO8 function */ + WIFI_IOT_IO_FUNC_GPIO_8_GPIO, + /** UART1 RTS function */ + WIFI_IOT_IO_FUNC_GPIO_8_UART1_RTS_N = 2, + /** SPI0 TXD function */ + WIFI_IOT_IO_FUNC_GPIO_8_SPI0_TXD, + /** PWM1 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_8_PWM1_OUT = 5, + /** I2S0 WS function */ + WIFI_IOT_IO_FUNC_GPIO_8_I2S0_WS, + /** WLAN ACTIVE function */ + WIFI_IOT_IO_FUNC_GPIO_8_WLAN_ACTIVE, +} WifiIotIoFuncGpio8; + +/** + * @brief Enumerates the functions of GPIO hardware pin 9. + */ +typedef enum { + /** GPIO9 function */ + WIFI_IOT_IO_FUNC_GPIO_9_GPIO, + /** I2C0 SCL function */ + WIFI_IOT_IO_FUNC_GPIO_9_I2C0_SCL, + /** UART2 RTS function */ + WIFI_IOT_IO_FUNC_GPIO_9_UART2_RTS_N, + /** SDIO D2 function */ + WIFI_IOT_IO_FUNC_GPIO_9_SDIO_D2, + /** SPI0 TXD function */ + WIFI_IOT_IO_FUNC_GPIO_9_SPI0_TXD, + /** PWM0 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT, + /** I2S0 MCLK function */ + WIFI_IOT_IO_FUNC_GPIO_9_I2S0_MCLK = 7, +} WifiIotIoFuncGpio9; + +/** + * @brief Enumerates the functions of GPIO hardware pin 10. + */ +typedef enum { + /** GPIO10 function */ + WIFI_IOT_IO_FUNC_GPIO_10_GPIO, + /** I2C0 SDA function */ + WIFI_IOT_IO_FUNC_GPIO_10_I2C0_SDA, + /** UART2 CTS function */ + WIFI_IOT_IO_FUNC_GPIO_10_UART2_CTS_N, + /** SDIO D3 function */ + WIFI_IOT_IO_FUNC_GPIO_10_SDIO_D3, + /** SPI0 CK function */ + WIFI_IOT_IO_FUNC_GPIO_10_SPI0_CK, + /** PWM1 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_10_PWM1_OUT, + /** I2S0 TX function */ + WIFI_IOT_IO_FUNC_GPIO_10_I2S0_TX = 7, +} WifiIotIoFuncGpio10; + +/** + * @brief Enumerates the functions of GPIO hardware pin 11. + */ +typedef enum { + /** GPIO11 function */ + WIFI_IOT_IO_FUNC_GPIO_11_GPIO, + /** UART2 TXD function */ + WIFI_IOT_IO_FUNC_GPIO_11_UART2_TXD = 2, + /** SDIO CMD function */ + WIFI_IOT_IO_FUNC_GPIO_11_SDIO_CMD, + /** SDIO RXD function */ + WIFI_IOT_IO_FUNC_GPIO_11_SPI0_RXD, + /** PWM2 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_11_PWM2_OUT, + /** RF TX_EN_EXT function */ + WIFI_IOT_IO_FUNC_GPIO_11_RF_TX_EN_EXT, + /** I2S0 RX function */ + WIFI_IOT_IO_FUNC_GPIO_11_I2S0_RX, +} WifiIotIoFuncGpio11; + +/** + * @brief Enumerates the functions of GPIO hardware pin 12. + */ +typedef enum { + /** GPIO12 function */ + WIFI_IOT_IO_FUNC_GPIO_12_GPIO, + /** SUART2 RXD function */ + WIFI_IOT_IO_FUNC_GPIO_12_UART2_RXD = 2, + /** SDIO CLK function */ + WIFI_IOT_IO_FUNC_GPIO_12_SDIO_CLK, + /** SDIO CSN function */ + WIFI_IOT_IO_FUNC_GPIO_12_SPI0_CSN, + /** PWM3 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_12_PWM3_OUT, + /** RF RX_EN_EXT function */ + WIFI_IOT_IO_FUNC_GPIO_12_RF_RX_EN_EXT, + /** I2S0 BCLK function */ + WIFI_IOT_IO_FUNC_GPIO_12_I2S0_BCLK, +} WifiIotIoFuncGpio12; + +/** + * @brief Enumerates the functions of GPIO hardware pin 13. + */ +typedef enum { + /** SSI DATA function */ + WIFI_IOT_IO_FUNC_GPIO_13_SSI_DATA, + /** UART0 TXD function */ + WIFI_IOT_IO_FUNC_GPIO_13_UART0_TXD, + /** UART2 RTS function */ + WIFI_IOT_IO_FUNC_GPIO_13_UART2_RTS_N, + /** SDIO D0 function */ + WIFI_IOT_IO_FUNC_GPIO_13_SDIO_D0, + /** GPIO13 function */ + WIFI_IOT_IO_FUNC_GPIO_13_GPIO, + /** PWM4 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_13_PWM4_OUT, + /** I2C0 SDA function */ + WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA, + /** I2S0 WS function */ + WIFI_IOT_IO_FUNC_GPIO_13_I2S0_WS, +} WifiIotIoFuncGpio13; + +/** + * @brief Enumerates the functions of GPIO hardware pin 14. + */ +typedef enum { + /** SSI CLK function */ + WIFI_IOT_IO_FUNC_GPIO_14_SSI_CLK, + /** UART0 RXD function */ + WIFI_IOT_IO_FUNC_GPIO_14_UART0_RXD, + /** UART2 CTS function */ + WIFI_IOT_IO_FUNC_GPIO_14_UART2_CTS_N, + /** SDIO D1 function */ + WIFI_IOT_IO_FUNC_GPIO_14_SDIO_D1, + /** GPIO14 function */ + WIFI_IOT_IO_FUNC_GPIO_14_GPIO, + /** PWM5 OUT function */ + WIFI_IOT_IO_FUNC_GPIO_14_PWM5_OUT, + /** I2C0 SCL function */ + WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL, +} WifiIotIoFuncGpio14; + +/** + * @brief Enumerates the functions of the SFC_CSN pin. + */ +typedef enum { + /** SFC CSN function (default value) */ + WIFI_IOT_IO_FUNC_SFC_CSN_SFC_CSN, + /** SDIO D2 function */ + WIFI_IOT_IO_FUNC_SFC_CSN_SDIO_D2, + /** GPIO9 function */ + WIFI_IOT_IO_FUNC_SFC_CSN_GPIO9, + /** SPI1 TXD function */ + WIFI_IOT_IO_FUNC_SFC_CSN_SPI1_TXD = 4, +} WifiIotIOFuncSfcCsn; + +/** + * @brief Enumerates the functions of the SFC_DI pin. + */ +typedef enum { + /** SFC DI function (default value) */ + WIFI_IOT_IO_FUNC_SFC_IO_0_SFC_DI, + /** SDIO CLK function */ + WIFI_IOT_IO_FUNC_SFC_IO_0_SDIO_CLK, + /** GPIO12 function */ + WIFI_IOT_IO_FUNC_SFC_IO_0_GPIO12, + /** RF RX_EN_EXT function */ + WIFI_IOT_IO_FUNC_SFC_IO_0_RF_RX_EN_EXT, + /** SPI0 CSN function */ + WIFI_IOT_IO_FUNC_SFC_IO_1_SPI0_CSN, +} WifiIotIoFuncSfcIo0; + +/** + * @brief Enumerates the functions of the SFC_DO pin. + */ +typedef enum { + /** SFC D0 function (default value) */ + WIFI_IOT_IO_FUNC_SFC_IO_1_SFC_DO, + /** SDIO D3 function */ + WIFI_IOT_IO_FUNC_SFC_IO_1_SDIO_D3, + /** GPIO10 function */ + WIFI_IOT_IO_FUNC_SFC_IO_1_GPIO10, + /** SPI0 CK function */ + WIFI_IOT_IO_FUNC_SFC_IO_1_SPI0_CK = 4, +} WifiIotIoFuncSfcIo1; + +/** + * @brief Enumerates the functions of the SFC_WPN pin. + */ +typedef enum { + /** SFC WPN function (default value) */ + WIFI_IOT_IO_FUNC_SFC_IO_2_SFC_WPN, + /** SDIO CMD function */ + WIFI_IOT_IO_FUNC_SFC_IO_2_SDIO_CMD, + /** GPIO11 function */ + WIFI_IOT_IO_FUNC_SFC_IO_2_GPIO11, + /** RF TX_EN_EXT function */ + WIFI_IOT_IO_FUNC_SFC_IO_2_RF_TX_EN_EXT, + /** SPI0 RXD function */ + WIFI_IOT_IO_FUNC_SFC_IO_1_SPI0_RXD, +} WifiIotIoFuncSfcIo2; + +/** + * @brief Enumerates the functions of the SFC_HOLDN pin. + */ +typedef enum { + /** SFC HOLDN function (default value) */ + WIFI_IOT_IO_FUNC_SFC_IO_3_SFC_HOLDN, + /** SDIO D1 function */ + WIFI_IOT_IO_FUNC_SFC_IO_3_SDIO_D1, + /** GPIO14 function */ + WIFI_IOT_IO_FUNC_SFC_IO_3_GPIO14, + /** SSI CLK function */ + WIFI_IOT_IO_FUNC_SFC_IO_3_SSI_CLK = 4, +} WifiIotIoFuncSfcIo3; + +/** + * @brief Enumerates the functions of the SFC_CLK pin. + */ +typedef enum { + /** SFC CLK function (default value) */ + WIFI_IOT_IO_FUNC_SFC_CLK_SFC_CLK, + /** SDIO D0 function */ + WIFI_IOT_IO_FUNC_SFC_CLK_SDIO_D0, + /** GPIO13 function */ + WIFI_IOT_IO_FUNC_SFC_CLK_GPIO13, + /** SSI DATA function */ + WIFI_IOT_IO_FUNC_SFC_CLK_SSI_DATA = 4, +} WifiIotIoFuncSfcClk; + +/** + * @brief Enumerates I/O driver strength levels. + * + */ +typedef enum { + /** Driver strength level 0 (highest) */ + WIFI_IOT_IO_DRIVER_STRENGTH_0 = 0, + /** Driver strength level 1 */ + WIFI_IOT_IO_DRIVER_STRENGTH_1, + /** Driver strength level 2 */ + WIFI_IOT_IO_DRIVER_STRENGTH_2, + /** Driver strength level 3 */ + WIFI_IOT_IO_DRIVER_STRENGTH_3, + /** Driver strength level 4 */ + WIFI_IOT_IO_DRIVER_STRENGTH_4, + /** Driver strength level 5 */ + WIFI_IOT_IO_DRIVER_STRENGTH_5, + /** Driver strength level 6 */ + WIFI_IOT_IO_DRIVER_STRENGTH_6, + /** Driver strength level 7 (lowest) */ + WIFI_IOT_IO_DRIVER_STRENGTH_7, + /** Maximum value */ + WIFI_IOT_IO_DRIVER_STRENGTH_MAX, +} WifiIotIoDriverStrength; + +/** + * @brief Enumerates GPIO pull-up or pull-down settings. + */ +typedef enum { + /** No pull */ + WIFI_IOT_IO_PULL_NONE, + /** Pull-up */ + WIFI_IOT_IO_PULL_UP, + /** Pull-down */ + WIFI_IOT_IO_PULL_DOWN, + /** Maximum value */ + WIFI_IOT_IO_PULL_MAX, +} WifiIotIoPull; + +/** + * @brief Sets the pull for a GPIO pin. + * + * @param id Indicates the GPIO pin. + * @param val Indicates the pull-up or pull-down to set. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoSetPull(WifiIotIoName id, WifiIotIoPull val); + +/** + * @brief Sets the multiplexing function for a GPIO pin. + * + * @param id Indicates the GPIO pin. + * @param val Indicates the I/O multiplexing function. For example, + * if the value of id is {@link WIFI_IOT_IO_NAME_GPIO_0}, + * the value type of val is {@link WifiIotIoFuncGpio0}. + * If the value of id is {@link WIFI_IOT_IO_NAME_GPIO_1}, + * the value type of val is {@link WifiIotIoFuncGpio1}. + * The same rule applies to other values. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoSetFunc(WifiIotIoName id, unsigned char val); + +/** + * @brief Obtains the pull type of a GPIO pin. + * + * @param id Indicates the GPIO pin. + * @param val Indicates the pointer to the address where the pull type is to be stored. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoGetPull(WifiIotIoName id, WifiIotIoPull *val); + +/** + * @brief Obtains the multiplexing function for a GPIO pin. + * + * @param id Indicates the GPIO pin. + * @param val Indicates the pointer to the address whether I/O multiplexing function is to be stored. + * For example, if the value of id is {@link WIFI_IOT_IO_NAME_GPIO_0}, + * the value type of val is {@link WifiIotIoFuncGpio0}. + * If the value of id is {@link WIFI_IOT_IO_NAME_GPIO_1}, + * the value type of val is {@link WifiIotIoFuncGpio1}. + * The same rule applies to other values. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IoGetFunc(WifiIotIoName id, unsigned char *val); + +/** + * @brief Sets the driver strength of a GPIO pin. + * + * @param id Indicates the GPIO pin. + * @param val Indicates the pointer to the address + * where the I/O driver strength level is to be stored. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IOGetDriverStrength(WifiIotIoName id, WifiIotIoDriverStrength *val); + +/** + * @brief Obtains the driver strength of a GPIO pin. + * + * @param id Indicates the GPIO pin. + * @param val Indicates the I/O driver strength level obtained. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int IOSetDriverStrength(WifiIotIoName id, WifiIotIoDriverStrength val); + +#endif +/** @} */ diff --git a/include/wifiiot_i2c.h b/include/wifiiot_i2c.h new file mode 100644 index 0000000000000000000000000000000000000000..2a292a6facf87bc2c3acaa213d4249133250fc8b --- /dev/null +++ b/include/wifiiot_i2c.h @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup wifiiot + * @{ + * + * @brief Provides dedicated device operation interfaces on the Wi-Fi module, + * including ADC, AT, flash, GPIO, I2C, I2S, partition, PWM, SDIO, UART, and watchdog. + * + * + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file wifiiot_i2c.h + * + * @brief Declares the I2C interface functions. + * + * These functions are used for I2C initialization and data transmission. \n + * + * @since 1.0 + * @version 1.0 + */ + +#ifndef WIFIIOT_I2C_H +#define WIFIIOT_I2C_H + +/** + * @brief Defines I2C data transmission attributes. + */ +typedef struct { + /** Pointer to the buffer storing data to send */ + unsigned char *sendBuf; + /** Length of data to send */ + unsigned int sendLen; + /** Pointer to the buffer for storing data to receive */ + unsigned char *receiveBuf; + /** Length of data received */ + unsigned int receiveLen; +} WifiIotI2cData; + +/** + * @brief Enumerates I2C hardware indexes. + */ +typedef enum { + /** I2C hardware index 0 */ + WIFI_IOT_I2C_IDX_0, + /** I2C hardware index 1 */ + WIFI_IOT_I2C_IDX_1, +} WifiIotI2cIdx; + +/** + * @brief Initializes an I2C device with a specified baud rate. + * + * + * + * @param id Indicates the I2C device ID. + * @param baudrate Indicates the I2C baud rate. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int I2cInit(WifiIotI2cIdx id, unsigned int baudrate); + +/** + * @brief Deinitializes an I2C device. + * + * @param id Indicates the I2C device ID. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int I2cDeinit(WifiIotI2cIdx id); + +/** + * @brief Writes data to an I2C device. + * + * + * + * @param id Indicates the I2C device ID. + * @param deviceAddr Indicates the I2C device address. + * @param i2cData Indicates the pointer to the data descriptor to write. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int I2cWrite(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData); + +/** + * @brief Reads data from an I2C device. + * + * The data read will be saved to the address specified by i2cData. + * + * @param id Indicates the I2C device ID. + * @param deviceAddr Indicates the I2C device address. + * @param i2cData Indicates the pointer to the data descriptor to read. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int I2cRead(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData); + +#endif +/** @} */ + diff --git a/include/wifiiot_i2c_ex.h b/include/wifiiot_i2c_ex.h new file mode 100644 index 0000000000000000000000000000000000000000..fe507c07f4f6391a3a284c4c8e3deb636391b504 --- /dev/null +++ b/include/wifiiot_i2c_ex.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * @addtogroup wifiiot + * @{ + * + * @brief Provides dedicated device operation interfaces on the Wi-Fi module, + * including ADC, AT, flash, GPIO, I2C, I2S, partition, PWM, SDIO, UART, and watchdog. + * + * + * + * @since 1.0 + * @version 1.0 + */ + +/** + * @file wifiiot_i2c_ex.h + * + * @brief Declares the extended I2C interface functions. + * + * These functions are used for I2C baud rate setting and device exception callback. \n + * + * @since 1.0 + * @version 1.0 + */ + +#ifndef WIFIIOT_I2C_EX_H +#define WIFIIOT_I2C_EX_H + +#include "wifiiot_i2c.h" + +/** + * @brief Indicates the callback invoked when a device exception occurs. + * + */ +typedef void (*I2CResetFunc)(void); +/** + * @brief Indicates the callback invoked for device preparation. + * + */ +typedef void (*I2cPrepareFunc)(void); +/** + * @brief Indicates the callback invoked for device recovery. + * + */ +typedef void (*I2cRestoreFunc)(void); + +/** + * @brief Defines I2C callbacks. + * + * @since 1.0 + * @version 1.0 + */ +typedef struct { + /** Callback invoked upon an I2C device exception */ + I2CResetFunc resetFunc; + /** Callback invoked for data preparation */ + I2cPrepareFunc prepareFunc; + /** Callback invoked for data recovery */ + I2cRestoreFunc restoreFunc; +} WifiIotI2cFunc; + +/** + * @brief Sends data to and receives data responses from an I2C device. + * + * + * + * @param id Indicates the I2C device ID. + * @param deviceAddr Indicates the I2C device address. + * @param i2cData Indicates the pointer to the device descriptor of the data to receive. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int I2cWriteread(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData); + +/** + * @brief Registers an I2C callback. + * + * @param id Indicates the I2C device ID. + * @param pfn Indicates the type of the callback to register. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +void I2cRegisterResetBusFunc(WifiIotI2cIdx id, WifiIotI2cFunc pfn); + +/** + * @brief Sets the baud rate for an I2C device. + * + * + * + * @param id Indicates the I2C device ID. + * @param baudrate Indicates the I2C baud rate to set. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + * @since 1.0 + * @version 1.0 + */ +unsigned int I2cSetBaudrate(WifiIotI2cIdx id, unsigned int baudrate); + +#endif +/** @} */