/*
 * @Author: zhy
 * @Date: 2022-09-09 18:25:41
 * @LastEditors: zhanghongyang zhanghy@bmser.com
 * @LastEditTime: 2023-10-17 11:13:19
 * @Description: 
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "os_inf.h"
#include "bms_debug.h"
#include "update_suport.h"
#include "platmacro.h"
#include "mosquitto.h"
#include "bmser_mqtt.h"
#include "exchange_with_mqtt.h"
#include "bmser_db_control_db.h"

void* runnable_thread(void* threadid)
{
    Std_Ret_Type ret = 0;
    ret = UpdateSup_Init();
    if (ret != Std_Ret_OK) {
        Debug_Printf("upsp init error \n");
        pthread_exit(NULL);
    }
    while (1) {
        UpdateSup_Runnable();
    }
    Debug_Printf("upsp runnable encount error \n");
    pthread_exit(NULL);
    return 0;
}

void* exchange_with_mqtt_thread(void* threadid)
{
    exchange_with_mqtt_callback_s exchange_with_mqtt_cbs = {
        .message_callback   = Update_Recv_XNCan_PayLoad,
        .start_upgrade      = update_start_new_otajob,
        .stop_upgrade       = UpdateSup_AbortUpdating,
        .show_version       = update_send_test_show_version,
        .dev_reset          = update_send_dev_reset,
    };
    exchange_with_mqtt_init(&exchange_with_mqtt_cbs, NULL);
    return 0;
}

int main(int argc, char* argv[])
{
    int tid;
    long user_arg;
    pthread_t runnable_threads;

    int32_t ret = BmserProtocol_DBInit(1);
    if (0 != ret) {
        Debug_Printf("BmserProtocol_DBInit fail. \r\n");
        return ret;
    }

    tid = pthread_create(&runnable_threads, NULL, runnable_thread, (void*)&user_arg);
    if (tid < 0) {
        Debug_Printf("runnable thread create failed \r\n");
    }

#if 0
    tid = pthread_create(&mqtt_threads, NULL, exchange_with_mqtt_thread, (void*)&arg);
    if (tid < 0) {
        Debug_Printf("mqtt thread create failed \r\n");
    }

    while (1) {
        sleep(100);
        exchange_with_mqtt_send_package(0, 0, 0);
    }
#else
    exchange_with_mqtt_thread(NULL);
#endif

    while (1) {
        sleep(1);
    }
}

