CURL_EXTERN CURLcode curl_easy_perform(CURL *curl);

1.函数原型

#ifdef  __cplusplus

extern "C" {

#endif

// ...

CURL_EXTERN CURLcode curl_easy_perform(CURL *curl);

// ...

#ifdef  __cplusplus

}

#endif

2. 枚举类型CURLcode的枚举值

typedef enum {
  CURLE_OK = 0,
  CURLE_UNSUPPORTED_PROTOCOL,    /* 1 */
  CURLE_FAILED_INIT,             /* 2 */
  CURLE_URL_MALFORMAT,           /* 3 */
  CURLE_NOT_BUILT_IN,            /* 4 - [was obsoleted in August 2007 for
                                    7.17.0, reused in April 2011 for 7.21.5] */
  CURLE_COULDNT_RESOLVE_PROXY,   /* 5 */
  CURLE_COULDNT_RESOLVE_HOST,    /* 6 */
  CURLE_COULDNT_CONNECT,         /* 7 */
  CURLE_WEIRD_SERVER_REPLY,      /* 8 */
  CURLE_REMOTE_ACCESS_DENIED,    /* 9 a service was denied by the server
                                    due to lack of access - when login fails
                                    this is not returned. */
  CURLE_FTP_ACCEPT_FAILED,       /* 10 - [was obsoleted in April 2006 for
                                    7.15.4, reused in Dec 2011 for 7.24.0]*/
  CURLE_FTP_WEIRD_PASS_REPLY,    /* 11 */
  CURLE_FTP_ACCEPT_TIMEOUT,      /* 12 - timeout occurred accepting server
                                    [was obsoleted in August 2007 for 7.17.0,
                                    reused in Dec 2011 for 7.24.0]*/
  CURLE_FTP_WEIRD_PASV_REPLY,    /* 13 */
  CURLE_FTP_WEIRD_227_FORMAT,    /* 14 */
  CURLE_FTP_CANT_GET_HOST,       /* 15 */
  CURLE_HTTP2,                   /* 16 - A problem in the http2 framing layer.
                                    [was obsoleted in August 2007 for 7.17.0,
                                    reused in July 2014 for 7.38.0] */
  CURLE_FTP_COULDNT_SET_TYPE,    /* 17 */
  CURLE_PARTIAL_FILE,            /* 18 */
  CURLE_FTP_COULDNT_RETR_FILE,   /* 19 */
  CURLE_OBSOLETE20,              /* 20 - NOT USED */
  CURLE_QUOTE_ERROR,             /* 21 - quote command failure */
  CURLE_HTTP_RETURNED_ERROR,     /* 22 */
  CURLE_WRITE_ERROR,             /* 23 */
  CURLE_OBSOLETE24,              /* 24 - NOT USED */
  CURLE_UPLOAD_FAILED,           /* 25 - failed upload "command" */
  CURLE_READ_ERROR,              /* 26 - couldn't open/read from file */
  CURLE_OUT_OF_MEMORY,           /* 27 */
  /* Note: CURLE_OUT_OF_MEMORY may sometimes indicate a conversion error
           instead of a memory allocation error if CURL_DOES_CONVERSIONS
           is defined
  */
  CURLE_OPERATION_TIMEDOUT,      /* 28 - the timeout time was reached */
  CURLE_OBSOLETE29,              /* 29 - NOT USED */
  CURLE_FTP_PORT_FAILED,         /* 30 - FTP PORT operation failed */
  CURLE_FTP_COULDNT_USE_REST,    /* 31 - the REST command failed */
  CURLE_OBSOLETE32,              /* 32 - NOT USED */
  CURLE_RANGE_ERROR,             /* 33 - RANGE "command" didn't work */
  CURLE_HTTP_POST_ERROR,         /* 34 */
  CURLE_SSL_CONNECT_ERROR,       /* 35 - wrong when connecting with SSL */
  CURLE_BAD_DOWNLOAD_RESUME,     /* 36 - couldn't resume download */
  CURLE_FILE_COULDNT_READ_FILE,  /* 37 */
  CURLE_LDAP_CANNOT_BIND,        /* 38 */
  CURLE_LDAP_SEARCH_FAILED,      /* 39 */
  CURLE_OBSOLETE40,              /* 40 - NOT USED */
  CURLE_FUNCTION_NOT_FOUND,      /* 41 - NOT USED starting with 7.53.0 */
  CURLE_ABORTED_BY_CALLBACK,     /* 42 */
  CURLE_BAD_FUNCTION_ARGUMENT,   /* 43 */
  CURLE_OBSOLETE44,              /* 44 - NOT USED */
  CURLE_INTERFACE_FAILED,        /* 45 - CURLOPT_INTERFACE failed */
  CURLE_OBSOLETE46,              /* 46 - NOT USED */
  CURLE_TOO_MANY_REDIRECTS,      /* 47 - catch endless re-direct loops */
  CURLE_UNKNOWN_OPTION,          /* 48 - User specified an unknown option */
  CURLE_TELNET_OPTION_SYNTAX,    /* 49 - Malformed telnet option */
  CURLE_OBSOLETE50,              /* 50 - NOT USED */
  CURLE_PEER_FAILED_VERIFICATION, /* 51 - peer's certificate or fingerprint
                                     wasn't verified fine */
  CURLE_GOT_NOTHING,             /* 52 - when this is a specific error */
  CURLE_SSL_ENGINE_NOTFOUND,     /* 53 - SSL crypto engine not found */
  CURLE_SSL_ENGINE_SETFAILED,    /* 54 - can not set SSL crypto engine as
                                    default */
  CURLE_SEND_ERROR,              /* 55 - failed sending network data */
  CURLE_RECV_ERROR,              /* 56 - failure in receiving network data */
  CURLE_OBSOLETE57,              /* 57 - NOT IN USE */
  CURLE_SSL_CERTPROBLEM,         /* 58 - problem with the local certificate */
  CURLE_SSL_CIPHER,              /* 59 - couldn't use specified cipher */
  CURLE_SSL_CACERT,              /* 60 - problem with the CA cert (path?) */
  CURLE_BAD_CONTENT_ENCODING,    /* 61 - Unrecognized/bad encoding */
  CURLE_LDAP_INVALID_URL,        /* 62 - Invalid LDAP URL */
  CURLE_FILESIZE_EXCEEDED,       /* 63 - Maximum file size exceeded */
  CURLE_USE_SSL_FAILED,          /* 64 - Requested FTP SSL level failed */
  CURLE_SEND_FAIL_REWIND,        /* 65 - Sending the data requires a rewind
                                    that failed */
  CURLE_SSL_ENGINE_INITFAILED,   /* 66 - failed to initialise ENGINE */
  CURLE_LOGIN_DENIED,            /* 67 - user, password or similar was not
                                    accepted and we failed to login */
  CURLE_TFTP_NOTFOUND,           /* 68 - file not found on server */
  CURLE_TFTP_PERM,               /* 69 - permission problem on server */
  CURLE_REMOTE_DISK_FULL,        /* 70 - out of disk space on server */
  CURLE_TFTP_ILLEGAL,            /* 71 - Illegal TFTP operation */
  CURLE_TFTP_UNKNOWNID,          /* 72 - Unknown transfer ID */
  CURLE_REMOTE_FILE_EXISTS,      /* 73 - File already exists */
  CURLE_TFTP_NOSUCHUSER,         /* 74 - No such user */
  CURLE_CONV_FAILED,             /* 75 - conversion failed */
  CURLE_CONV_REQD,               /* 76 - caller must register conversion
                                    callbacks using curl_easy_setopt options
                                    CURLOPT_CONV_FROM_NETWORK_FUNCTION,
                                    CURLOPT_CONV_TO_NETWORK_FUNCTION, and
                                    CURLOPT_CONV_FROM_UTF8_FUNCTION */
  CURLE_SSL_CACERT_BADFILE,      /* 77 - could not load CACERT file, missing
                                    or wrong format */
  CURLE_REMOTE_FILE_NOT_FOUND,   /* 78 - remote file not found */
  CURLE_SSH,                     /* 79 - error from the SSH layer, somewhat
                                    generic so the error message will be of
                                    interest when this has happened */

  CURLE_SSL_SHUTDOWN_FAILED,     /* 80 - Failed to shut down the SSL
                                    connection */
  CURLE_AGAIN,                   /* 81 - socket is not ready for send/recv,
                                    wait till it's ready and try again (Added
                                    in 7.18.2) */
  CURLE_SSL_CRL_BADFILE,         /* 82 - could not load CRL file, missing or
                                    wrong format (Added in 7.19.0) */
  CURLE_SSL_ISSUER_ERROR,        /* 83 - Issuer check failed.  (Added in
                                    7.19.0) */
  CURLE_FTP_PRET_FAILED,         /* 84 - a PRET command failed */
  CURLE_RTSP_CSEQ_ERROR,         /* 85 - mismatch of RTSP CSeq numbers */
  CURLE_RTSP_SESSION_ERROR,      /* 86 - mismatch of RTSP Session Ids */
  CURLE_FTP_BAD_FILE_LIST,       /* 87 - unable to parse FTP file list */
  CURLE_CHUNK_FAILED,            /* 88 - chunk callback reported error */
  CURLE_NO_CONNECTION_AVAILABLE, /* 89 - No connection available, the
                                    session will be queued */
  CURLE_SSL_PINNEDPUBKEYNOTMATCH, /* 90 - specified pinned public key did not
                                     match */
  CURLE_SSL_INVALIDCERTSTATUS,   /* 91 - invalid certificate status */
  CURLE_HTTP2_STREAM,            /* 92 - stream error in HTTP/2 framing layer
                                    */
  CURLE_RECURSIVE_API_CALL,      /* 93 - an api function was called from
                                    inside a callback */
  CURL_LAST /* never use! */
} CURLcode;

分析 /*************************************************************************************************/ /* INCLUDE_FILES */ /*************************************************************************************************/ #include "weather_server.h" /**************************************************************************************************/ /* DEFINES */ /**************************************************************************************************/ #define ME "weather" /**************************************************************************************************/ /* TYPES */ /**************************************************************************************************/ typedef struct { int temperature; int temperatureMax; int temperatureMin; int houridx; char weatherType[10]; struct { int temperature; char weatherType[10]; } forecast6Hours[6]; } realtime_weather_info_struct; typedef struct { int temperatureMax; int temperatureMin; char dayWeatherType[10]; char nightWeatherType[10]; } forecast_weather_info_struct; /*************************************************************************************************/ /* EXTERN_PROTOTYPES */ /*************************************************************************************************/ /*************************************************************************************************/ /* LOCAL_PROTOTYPES */ /*************************************************************************************************/ /*************************************************************************************************/ /* VARIABLES */ /*************************************************************************************************/ static amxd_dm_t *g_pdm = NULL; static amxo_parser_t *g_pparser = NULL; static uloop_timeout g_timer = { .cb = NULL }; realtime_weather_info_struct g_realtime_weather; forecast_weather_info_struct g_forecast_weather[3]; int g_errorcode = 0; char g_errormsg[20] = ""; /*************************************************************************************************/ /* LOCAL_FUNCTIONS */ /*************************************************************************************************/ const char* get_temperature_unit() { static const char* default_unit = "fahrenheit"; amxd_object_t *pobj_weather = NULL; amxd_object_t *pobj_config = NULL; const char* unit = NULL; pobj_weather = amxd_dm_findf(g_pdm, "X_TP_Weather"); if (!pobj_weather) { SAH_TRACEZ_WARN(ME, "Failed to find X_TP_Weather object."); return default_unit; } pobj_config = amxd_object_get_child(pobj_weather, "Config"); if (!pobj_config) { SAH_TRACEZ_WARN(ME, "Failed to find X_TP_Weather.Config."); return default_unit; } amxc_var_t unit_var; amxc_var_init(&unit_var); if (amxd_object_get_value(pobj_config, "TemperatureUnit", &unit_var) != 0) { SAH_TRACEZ_WARN(ME, "Failed to read TemperatureUnit from config."); amxc_var_clean(&unit_var); return default_unit; } unit = amxc_var_string(&unit_var); if (!unit || (strcmp(unit, "centigrade") != 0 && strcmp(unit, "fahrenheit") != 0)) { SAH_TRACEZ_WARN(ME, "Invalid temperature unit value: %s. Using default.", unit ? unit : "NULL"); amxc_var_clean(&unit_var); return default_unit; } amxc_var_clean(&unit_var); return unit; } int fahrenheit_to_celsius(int f) { return (f - 32) * 5 / 9; } int get_token_url(char *token, char *url) { int ret = -1; int retry_count = 3; amxc_var_t args; amxc_var_t rets; FILE *fp = NULL; amxc_var_init(&args); amxc_var_init(&rets); while (retry_count > 0) { if((fp = fopen(FILE_CLOUD_TOKEN_WEATHER, "r"))) { fscanf(fp, "%s", token); fscanf(fp, "%s", url); fclose(fp); ret = 0; break; } else { amxc_var_set_type(&args, AMXC_VAR_ID_HTABLE); amxc_var_set_type(&rets, AMXC_VAR_ID_HTABLE); amxc_var_add_key(cstring_t, &args, "servicetype", SERVICE_TYPE); amxc_var_add_key(uint32_t, &args, "retrycount", 3); ret = amxb_call(amxb_be_who_has(CLOUD_PATH), CLOUD_HTTPS_PATH, GET_DEVICETOKEN_FUNC, &args, &rets, 5); if (ret != 0) { SAH_TRACEZ_ERROR("cloud", "get device token failed! error_code=%u", GETP_UINT32(&rets, "0.error_code")); goto exit; } } retry_count--; } exit: amxc_var_clean(&args); amxc_var_clean(&rets); return ret; } static size_t OnWriteData_Post(void* buffer, size_t size, size_t nmemb, void* lpVoid) { unsigned int len = (unsigned int)size * (unsigned int)nmemb; char *str = malloc(len + 1); if (NULL == str || NULL == buffer) { return -1; } char* pData = buffer; memset(str, 0, len + 1); memcpy(str, pData, len); DEBUG("response: %s", str); char **response = lpVoid; *response = str; return len; } int get_location_key() { amxd_object_t *pobj_weather = amxd_dm_findf(g_pdm, "X_TP_Weather"); if (!pobj_weather) { SAH_TRACEZ_WARN(ME, "Failed to find X_TP_Weather object."); return -1; } amxc_var_t locationkey_var; amxc_var_init(&locationkey_var); if (amxd_object_get_value(pobj_weather, "LocationKey", &locationkey_var) != 0) { SAH_TRACEZ_WARN(ME, "Failed to read LocationKey."); return -1; } int locationkey = amxc_var_int(&locationkey_var); amxc_var_clean(&locationkey_var); return locationkey; } int get_realtime_info_from_cloud(realtime_weather_info_struct *realtime_weather_info, const char *device_token, const char *url_prefix) { json_object* response_json = NULL; json_object* errorcode = NULL; json_object* message = NULL; json_object* result = NULL; json_object* temperature = NULL; json_object* temperatureMax = NULL; json_object* temperatureMin = NULL; json_object* weatherType = NULL; json_object* forecast6Hours = NULL; json_object* hourlyForecats = NULL; json_object* j_code = NULL; int errorcode_data = -1; const char* message_data = NULL; int temperature_data = -1; int temperatureMax_data = -1; int temperatureMin_data = -1; int hourlyTemperature_data[6] = {0}; const char* weatherType_data = NULL; const char* hourlyWeatherType_data[6] = {NULL}; int i =0; int ret = 0; int code = -1; CURL *curl = NULL; struct curl_slist *headers = NULL; int loc_key = 0; char request_data[512] = {0}; char *response = NULL; char request_url[128] = {0}; CURLcode res; st_http_resinfo httpResInfo; SAH_TRACEZ_DEBUG(ME, "Getting real-time weather info from cloud..."); if (realtime_weather_info == NULL || device_token == NULL || url_prefix == NULL) { SAH_TRACEZ_ERROR(ME, "params error"); ret = -1; goto exit; } sprintf(request_url, "%s%s", url_prefix, REALTIME_URL_POSTFIX); curl = curl_easy_init(); if (!curl) { SAH_TRACEZ_ERROR(ME, "Failed to initialize cURL."); ret = -1; goto exit; } headers = curl_slist_append(headers, "Content-Type: application/json"); loc_key = get_location_key(); if (loc_key < 0) { SAH_TRACEZ_ERROR(ME, "Invalid location key."); ret = -1; goto exit; } snprintf(request_data, sizeof(request_data), "{\"locationKey\":%d,\"deviceToken\":\"%s\"}", loc_key, device_token); curl_easy_setopt(curl, CURLOPT_URL, request_url); curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_data); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData_Post); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1); curl_easy_setopt(curl, CURLOPT_CAINFO, CA_CRT_PATH); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, POST_TRANSFER_TIMEOUT); curl_easy_setopt(curl, CURLOPT_TIMEOUT, POST_CONNECT_TIMEOUT); //CURLOPT needs to be set_ FOLLOWLOCATION is 1, otherwise, the data after redirecting will not be returned curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); res = curl_easy_perform(curl); if (res != CURLE_OK) { SAH_TRACEZ_ERROR(ME, "cURL error: %s", curl_easy_strerror(res)); ret = -1; goto exit; } memset(&httpResInfo, 0, sizeof(st_http_resinfo)); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, httpResInfo.status_code); SAH_TRACEZ_DEBUG(ME, "HTTP Status Code: %ld", httpResInfo.status_code); if (httpResInfo.status_code != 200) { SAH_TRACEZ_WARN(ME, "Non-200 HTTP response received."); ret = -1; goto exit; } response_json = json_tokener_parse(response); j_code = json_object_object_get(response_json, "code"); code = json_object_get_int(j_code); if (0 != code) { SAH_TRACEZ_WARN(ME, "Cloud returned error code: %d", code); ret = -1; goto exit; } if (response == NULL) { ret = -1; goto exit; } message = json_object_object_get(response_json, "message"); result = json_object_object_get(response_json, "result"); temperature = json_object_object_get(result, "temperature"); temperatureMax = json_object_object_get(result, "temperatureMax"); temperatureMin = json_object_object_get(result, "temperatureMin"); weatherType = json_object_object_get(result, "weatherType"); message_data = json_object_get_string(message); temperature_data = json_object_get_int(temperature); temperatureMax_data = json_object_get_int(temperatureMax); temperatureMin_data = json_object_get_int(temperatureMin); weatherType_data = json_object_get_string(weatherType); forecast6Hours = json_object_object_get(result, "forecast6Hours"); if ( json_type_array != json_object_get_type(forecast6Hours) || 6 != json_object_array_length(forecast6Hours) ) { ret = -1; goto exit; } for ( i = 0; i < 6; i++) { hourlyForecats = json_object_array_get_idx(forecast6Hours, i); temperature = json_object_object_get(hourlyForecats, "temperature"); weatherType = json_object_object_get(hourlyForecats, "weatherType"); hourlyTemperature_data[i] = json_object_get_int(temperature); hourlyWeatherType_data[i] = json_object_get_string(weatherType); } if (message_data && weatherType_data) { if (0 == strcmp(get_temperature_unit(), "centigrade")) { realtime_weather_info->temperature = (temperature_data - 32) / 1.8; realtime_weather_info->temperatureMax = (temperatureMax_data -32) / 1.8; realtime_weather_info->temperatureMin = (temperatureMin_data - 32) / 1.8; for ( i = 0; i < 6; i++) { realtime_weather_info->forecast6Hours[i].temperature = (hourlyTemperature_data[i] - 32) / 1.8; } } else { realtime_weather_info->temperature = temperature_data; realtime_weather_info->temperatureMax = temperatureMax_data; realtime_weather_info->temperatureMin = temperatureMin_data; for ( i = 0; i < 6; i++) { realtime_weather_info->forecast6Hours[i].temperature = hourlyTemperature_data[i]; } } sprintf(realtime_weather_info->weatherType, "%s", weatherType_data); for ( i = 0; i < 6; i++) { sprintf(realtime_weather_info->forecast6Hours[i].weatherType, "%s", hourlyWeatherType_data[i]); } } SAH_TRACEZ_INFO(ME, "Realtime data updated successfully."); exit: if (response_json) { json_object_put(response_json); } curl_easy_cleanup(curl); free(response); return ret; } int get_forecast_info_from_cloud(forecast_weather_info_struct *forecast_weather_info, const char *device_token, const char *url_prefix) { json_object* response_json = NULL; json_object* errorcode = NULL; json_object* message = NULL; json_object* result = NULL; json_object* temperature = NULL; json_object* temperatureMax = NULL; json_object* temperatureMin = NULL; json_object* weatherType = NULL; json_object* forecastList = NULL; json_object* ListItem = NULL; json_object* dayWeatherType = NULL; json_object* nightWeatherType = NULL; int errorcode_data = -1; const char* message_data = NULL; int temperature_data = -1; int temperatureMax_data = -1; int temperatureMin_data = -1; const char* weatherType_data = NULL; const char* dayWeatherType_data = NULL; const char* nightWeatherType_data = NULL; int i =0; int ret = 0; CURL *curl = NULL; struct curl_slist *headers = NULL; int loc_key = 0; char request_data[512] = {0}; char *response = NULL; char request_url[128] = {0}; CURLcode res; st_http_resinfo httpResInfo; SAH_TRACEZ_DEBUG(ME, "Getting real-time weather info from cloud..."); if (forecast_weather_info == NULL || device_token == NULL || url_prefix == NULL) { SAH_TRACEZ_ERROR(ME, "params error"); ret = -1; goto exit; } sprintf(request_url, "%s%s", url_prefix, FORECASTS_URL_POSTFIX); curl = curl_easy_init(); if (!curl) { SAH_TRACEZ_ERROR(ME, "Failed to initialize cURL."); ret = -1; goto exit; } headers = curl_slist_append(headers, "Content-Type: application/json"); loc_key = get_location_key(); if (loc_key < 0) { SAH_TRACEZ_ERROR(ME, "Invalid location key."); ret = -1; goto exit; } snprintf(request_data, sizeof(request_data), "{\"locationKey\":%d,\"deviceToken\":\"%s\"}", loc_key, device_token); curl_easy_setopt(curl, CURLOPT_URL, request_url); curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_data); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData_Post); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // 10 seconds timeout curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1); curl_easy_setopt(curl, CURLOPT_CAINFO, CA_CRT_PATH); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, POST_TRANSFER_TIMEOUT); curl_easy_setopt(curl, CURLOPT_TIMEOUT, POST_CONNECT_TIMEOUT); //CURLOPT needs to be set_ FOLLOWLOCATION is 1, otherwise, the data after redirecting will not be returned curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); res = curl_easy_perform(curl); if (res != CURLE_OK) { SAH_TRACEZ_ERROR(ME, "cURL error: %s", curl_easy_strerror(res)); curl_easy_cleanup(curl); ret = -1; goto exit; } memset(&httpResInfo, 0, sizeof(st_http_resinfo)); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, httpResInfo.status_code); SAH_TRACEZ_DEBUG(ME, "HTTP Status Code: %ld", httpResInfo.status_code); if (httpResInfo.status_code != 200) { SAH_TRACEZ_WARN(ME, "Non-200 HTTP response received."); curl_easy_cleanup(curl); return -1; } if (response) { response_json = json_tokener_parse(response); errorcode = json_object_object_get(response_json, "code"); errorcode_data = json_object_get_int(errorcode); if (0 != errorcode_data) { SAH_TRACEZ_WARN(ME, "Cloud returned error code: %d", errorcode_data); ret = -1; goto exit; } message = json_object_object_get(response_json, "message"); result = json_object_object_get(response_json, "result"); forecastList = json_object_object_get(result, "forecastList"); message_data = json_object_get_string(message); if (message_data) { for ( i = 0; i < 3; i++) { ListItem = json_object_array_get_idx(forecastList,i); temperatureMax = json_object_object_get(ListItem, "temperatureMax"); temperatureMin = json_object_object_get(ListItem, "temperatureMin"); dayWeatherType = json_object_object_get(ListItem, "dayWeatherType"); nightWeatherType = json_object_object_get(ListItem, "nightWeatherType"); temperatureMax_data = json_object_get_int(temperatureMax); temperatureMin_data = json_object_get_int(temperatureMin); dayWeatherType_data = json_object_get_string(dayWeatherType); nightWeatherType_data = json_object_get_string(nightWeatherType); if (dayWeatherType_data && nightWeatherType_data) { if (0 == strcmp(get_temperature_unit(), "centigrade")) { forecast_weather_info[i].temperatureMax = (temperatureMax_data - 32) / 1.8; forecast_weather_info[i].temperatureMin = (temperatureMin_data - 32) / 1.8; } else { forecast_weather_info[i].temperatureMax = temperatureMax_data; forecast_weather_info[i].temperatureMin = temperatureMin_data; } sprintf(forecast_weather_info[i].dayWeatherType, "%s", dayWeatherType_data); sprintf(forecast_weather_info[i].nightWeatherType, "%s", nightWeatherType_data); } } ret = 0; } } SAH_TRACEZ_INFO(ME, "Forecast data updated successfully."); exit: if (response_json) { json_object_put(response_json); } curl_easy_cleanup(curl); free(response); return ret; } static int update_weather_url(void) { char device_token[MAX_TOKEN_LEN] = {0}; char cloud_url[MAX_URL_LEN] = {0}; int i = 0; memset(&g_realtime_weather, 0, sizeof(g_realtime_weather)); for (i = 0; i < 6; i++) { memset(&g_realtime_weather.forecast6Hours[i], 0, sizeof(g_realtime_weather.forecast6Hours[i])); } for (i = 0; i < 3; i++) { memset(&g_forecast_weather[i], 0, sizeof(g_forecast_weather[i])); } if (get_token_url(device_token, cloud_url) != 0) { SAH_TRACEZ_ERROR(ME, "Failed to retrieve token and URL."); return -1; } if (get_realtime_info_from_cloud(&g_realtime_weather, device_token, cloud_url) != 0) { SAH_TRACEZ_ERROR(ME, "Failed to fetch real-time weather data."); return -1; } else { g_realtime_weather.houridx = 0; /* 重置当前小时数 */ SAH_TRACEZ_INFO(ME, "success to fetch real-time weather data."); } if (get_forecast_info_from_cloud(g_forecast_weather, device_token, cloud_url) != 0) { SAH_TRACEZ_ERROR(ME, "Failed to fetch forecast weather data."); return -1; } SAH_TRACEZ_INFO(ME, "All weather data updated successfully."); return 0; } static void config_changed_cb(amxd_object_t *object, amxd_function_t *func, amxc_var_t *args, amxc_var_t *rets) { SAH_TRACEZ_INFO(ME, "Configuration changed. Re-fetching weather data."); update_weather_url(); } static void unit_change_handler(amxd_object_t *obj, amxd_function_t *func, amxc_var_t *args, amxc_var_t *rets) { SAH_TRACEZ_INFO(ME, "Temperature unit changed. Recalculating values."); if (strcmp(get_temperature_unit(), "centigrade") == 0) { g_realtime_weather.temperature = fahrenheit_to_celsius(g_realtime_weather.temperature); g_realtime_weather.temperatureMax = fahrenheit_to_celsius(g_realtime_weather.temperatureMax); g_realtime_weather.temperatureMin = fahrenheit_to_celsius(g_realtime_weather.temperatureMin); for (int i = 0; i < 3; ++i) { g_forecast_weather[i].temperatureMax = fahrenheit_to_celsius(g_forecast_weather[i].temperatureMax); g_forecast_weather[i].temperatureMin = fahrenheit_to_celsius(g_forecast_weather[i].temperatureMin); } } } int save_realtime_weather(realtime_weather_info_struct *weather) { amxd_object_t *pobj_weather = NULL; amxd_object_t *pobj_realtime = NULL; amxd_object_t *hourly_forecast = NULL; pobj_weather = amxd_dm_findf(g_pdm, "X_TP_Weather"); if (!pobj_weather) { SAH_TRACEZ_WARN(ME, "Failed to find X_TP_Weather object."); return -1; } pobj_realtime = amxd_object_get_child(pobj_weather, "Realtime"); if (!pobj_realtime) { SAH_TRACEZ_WARN(ME, "Failed to find X_TP_Weather.Realtime."); return -1; } amxd_object_set_value(uint32_t, pobj_realtime, "Temperature", weather->temperature); amxd_object_set_value(uint32_t, pobj_realtime, "TemperatureMax", weather->temperatureMax); amxd_object_set_value(uint32_t, pobj_realtime, "TemperatureMin", weather->temperatureMin); amxd_object_set_value(string, pobj_realtime, "WeatherType", weather->weatherType); for (int i = 0; i < 6; ++i) { char path[64]; snprintf(path, sizeof(path), "HourlyForecast[%d]", i); hourly_forecast = amxd_object_get_child(pobj_realtime, path); if (!hourly_forecast) { SAH_TRACEZ_WARN(ME, "Failed to find HourlyForecast[%d].", i); continue; } amxd_object_set_value(uint32_t, hourly_forecast, "HourIndex", i); amxd_object_set_value(uint32_t, hourly_forecast, "Temperature", weather->forecast6Hours[i].temperature); amxd_object_set_value(string, hourly_forecast, "WeatherType", weather->forecast6Hours[i].weatherType); } return 0; } int save_forecast_weather(forecast_weather_info_struct *forecasts) { amxd_object_t *pobj_weather = NULL; amxd_object_t *pobj_forecast = NULL; amxd_object_t *day_forecast = NULL; pobj_weather = amxd_dm_findf(g_pdm, "X_TP_Weather"); if (!pobj_weather) { SAH_TRACEZ_WARN(ME, "Failed to find X_TP_Weather object."); return -1; } pobj_forecast = amxd_object_get_child(pobj_weather, "Forecast"); if (!pobj_forecast) { SAH_TRACEZ_WARN(ME, "Failed to find X_TP_Weather.Forecast."); return -1; } for (int i = 0; i < 3; ++i) { char path[64]; snprintf(path, sizeof(path), "Days[%d]", i); day_forecast = amxd_object_get_child(pobj_forecast, path); if (!day_forecast) { SAH_TRACEZ_WARN(ME, "Failed to find Days[%d].", i); continue; } amxd_object_set_value(uint32_t, day_forecast, "DayOffset", i); amxd_object_set_value(uint32_t, day_forecast, "TemperatureMax", forecasts[i].temperatureMax); amxd_object_set_value(uint32_t, day_forecast, "TemperatureMin", forecasts[i].temperatureMin); amxd_object_set_value(string, day_forecast, "DayWeatherType", forecasts[i].dayWeatherType); amxd_object_set_value(string, day_forecast, "NightWeatherType", forecasts[i].nightWeatherType); } return 0; } static void interval_change_handler(amxd_object_t *obj, amxd_function_t *func, amxc_var_t *args, amxc_var_t *rets) { SAH_TRACEZ_INFO(ME, "Update interval changed. Adjusting timer."); uloop_timeout_set(&g_timer, 3600000); } amxd_status_t _weather_update_now(UNUSED amxd_object_t *object, UNUSED amxd_function_t *func, UNUSED amxc_var_t *args, UNUSED amxc_var_t *rets) { SAH_TRACEZ_INFO(ME, "Manual weather update triggered."); update_weather_url(); return amxd_status_ok; } amxd_status_t _weather_start_polling(UNUSED amxd_object_t *object, UNUSED amxd_function_t *func, UNUSED amxc_var_t *args, UNUSED amxc_var_t *rets) { SAH_TRACEZ_INFO(ME, "Starting periodic polling."); uloop_timeout_set(&g_timer, 3600000); // 1 hour return amxd_status_ok; } amxd_status_t _weather_stop_polling(UNUSED amxd_object_t *object, UNUSED amxd_function_t *func, UNUSED amxc_var_t *args, UNUSED amxc_var_t *rets) { SAH_TRACEZ_INFO(ME, "Stopping periodic polling."); uloop_timeout_cancel(&g_timer); return amxd_status_ok; } static void weather_timer_cb(struct uloop_timeout *timer) { SAH_TRACEZ_INFO(ME, "Periodic weather update triggered."); if (update_weather_url() != 0) { SAH_TRACEZ_ERROR(ME, "Failed to update weather data."); uloop_timeout_set(timer, 60000); } else { SAH_TRACEZ_INFO(ME, "Weather data updated successfully."); uloop_timeout_set(timer, 3600000); } } static int weather_init(amxd_dm_t *dm, amxo_parser_t *parser) { amxd_object_t *weather_obj = amxd_dm_findf(dm, "X_TP_Weather"); if (!weather_obj) { return -1; } amxd_object_t *config_obj = amxd_object_get_child(weather_obj, "Config"); if (!config_obj) { return -1; } amxd_object_set_value(uint32_t, config_obj, "LocationKey", 10001); amxd_object_set_value(string, config_obj, "TemperatureUnit", "fahrenheit"); amxd_object_set_value(uint32_t, config_obj, "UpdateInterval", 21600); amxd_object_t *status_obj = amxd_object_get_child(weather_obj, "Status"); if (!status_obj) { return -1; } amxd_object_set_value(bool, status_obj, "IsAvailable", false); amxd_object_set_value(string, status_obj, "LastUpdateTime", ""); amxd_object_set_value(string, status_obj, "CurrentStatus", "Idle"); amxd_object_set_value(uint32_t, status_obj, "ErrorCode", 0); amxd_object_t *realtime_obj = amxd_object_get_child(weather_obj, "Realtime"); if (!realtime_obj) { return -1; } amxd_object_set_value(uint32_t, realtime_obj, "Temperature", 0); amxd_object_set_value(uint32_t, realtime_obj, "TemperatureMax", 0); amxd_object_set_value(uint32_t, realtime_obj, "TemperatureMin", 0); amxd_object_set_value(string, realtime_obj, "WeatherType", ""); amxd_object_t *forecast_obj = amxd_object_get_child(weather_obj, "Forecast"); if (!forecast_obj) { return -1; } amxo_register_function(parser, "X_TP_Weather.UpdateNow", _weather_update_now, NULL); amxo_register_function(parser, "X_TP_Weather.StartPolling", _weather_start_polling, NULL); amxo_register_function(parser, "X_TP_Weather.StopPolling", _weather_stop_polling, NULL); return 0; } int main(int reason, amxd_dm_t *pdm, amxo_parser_t *pparser) { memset(&timer, 0, sizeof(timer)); switch (reason) { case AMXO_START: SAH_TRACEZ_INFO(ME, "Initializing weather module..."); weather_init(pdm, pparser); timer.cb = weather_timer_cb; uloop_timeout_set(&timer, 1000); SAH_TRACEZ_INFO(ME, "Weather module initialized."); break; case AMXO_STOP: SAH_TRACEZ_INFO(ME, "Cleaning up weather module..."); uloop_timeout_cancel(&timer); SAH_TRACEZ_INFO(ME, "Weather module cleaned up."); break; } return 0; }
最新发布
12-25
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> #include "cJSON.h" #include "depskmainCopy.h" #include "lvgl/lvgl.h" #include <stdio.h> #include <errno.h> #include "ui_helpers.h" #include "ui_events.h" #include "ui.h" #include "ui_helpers.h" // 使用缓冲区输入输出是没问题的,需要解决循环的问题, char textbuf[128] = "who are you"; extern char * kbEntertext; char dpOut[128] = {0}; // ================== 前置声明 ================== struct MemoryStruct; struct APIResponse; char* get_api_key(); struct APIResponse call_deepseek(const char *api_key, const char *prompt); void print_token_usage(int prompt_tokens, int completion_tokens, int total_tokens); DeepSeekSession *session; char input[1024]; int prompt_tokens, completion_tokens, total_tokens; char *response; // ================== 辅助函数 ================== // libcurl回调函数 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { size_t realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *)userp; char *ptr = realloc(mem->memory, mem->size + realsize + 1); if(!ptr) { fprintf(stderr, "内存分配错误\n"); return 0; } mem->memory = ptr; memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } // 获取API密钥 char* get_api_key() { char *api_key = getenv("DEEPSEEK_API_KEY"); if (api_key && strlen(api_key) > 0) { return strdup(api_key); } printf("未找到环境变量 DEEPSEEK_API_KEY\n"); printf("请访问 https://platform.deepseek.com/api-keys 创建API密钥\n"); char *input_key = malloc(256); if (!input_key) { fprintf(stderr, "内存分配失败\n"); return NULL; } printf("请输入您的DeepSeek API密钥: "); if (fgets(input_key, 256, stdin) == NULL) { free(input_key); return NULL; } input_key[strcspn(input_key, "\n")] = 0; return input_key; } // ================== 核心API函数 ================== // 调用DeepSeek API struct APIResponse call_deepseek(const char *api_key, const char *prompt) { CURL *curl; CURLcode res; struct MemoryStruct chunk = {0}; long http_code = 0; struct APIResponse api_response = {0}; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(!curl) { fprintf(stderr, "无法初始化cURL\n"); return api_response; } // 设置API端点 curl_easy_setopt(curl, CURLOPT_URL, "https://api.deepseek.com/chat/completions"); // 设置证书路径 // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // // 添加 SSL 版本和密码套件配置 // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "HIGH:!aNULL:!MD5"); // 设置 TLS 1.3 // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_3); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "TLS_AES_128_GCM_SHA256"); // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "DEFAULT@SECLEVEL=1"); // 添加SSL证书验证配置,禁用证书验证(仅用于测试) // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // 设置完整的证书链 // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // curl_easy_setopt(curl, CURLOPT_CAPATH, "/etc/ssl/certs"); // 配置SSL证书和验证选项 curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "DEFAULT"); curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/new_combined_certs.pem"); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // 验证配置 // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // 添加更多调试信息 // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // 添加详细的错误信息 char errbuf[CURL_ERROR_SIZE]; curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L); // 设置请求头 struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); char auth_header[256]; snprintf(auth_header, sizeof(auth_header), "Authorization: Bearer %s", api_key); headers = curl_slist_append(headers, auth_header); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 构建请求体 cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "model", "deepseek-chat"); cJSON_AddNumberToObject(root, "max_tokens", 2000); cJSON *messages = cJSON_CreateArray(); cJSON *message = cJSON_CreateObject(); cJSON_AddStringToObject(message, "role", "user"); cJSON_AddStringToObject(message, "content", prompt); cJSON_AddItemToArray(messages, message); cJSON_AddItemToObject(root, "messages", messages); char *post_data = cJSON_PrintUnformatted(root); // 设置POST数据 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(post_data)); // 设置响应回调 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); // 设置超时 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); // 执行请求 res = curl_easy_perform(curl); // 检查不支持的协议 if(res == CURLE_UNSUPPORTED_PROTOCOL) { fprintf(stderr, "cURL错误: 不支持的协议 - URL: %s\n", "https://api.deepseek.com/chat/completions"); // 获取支持的协议列表 curl_version_info_data *ver_info = curl_version_info(CURLVERSION_NOW); if(ver_info && ver_info->protocols) { fprintf(stderr, "支持的协议列表:\n"); const char * const *proto; for(proto = ver_info->protocols; *proto; proto++) { fprintf(stderr, "- %s\n", *proto); } } } else if(res != CURLE_OK) { fprintf(stderr, "cURL错误: %s\n", curl_easy_strerror(res)); fprintf(stderr, "详细错误: %s\n", errbuf); long connect_code; curl_easy_getinfo(curl, CURLINFO_HTTP_CONNECTCODE, &connect_code); fprintf(stderr, "连接状态码: %ld\n", connect_code); } else if (res == CURLE_SSL_PEER_CERTIFICATE) { fprintf(stderr, "SSL证书错误: %s\n", curl_easy_strerror(res)); // 获取更多错误信息 char error_buffer[CURL_ERROR_SIZE]; curl_easy_getinfo(curl, CURLINFO_SSL_VERIFYRESULT, &error_buffer); fprintf(stderr, "SSL验证结果: %s\n", error_buffer); } // 检查HTTP状态码 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); // 清理cURL资源 curl_easy_cleanup(curl); curl_slist_free_all(headers); cJSON_Delete(root); free(post_data); // 调试信息 printf("\n--- API请求详情 ---\n"); printf("端点: https://api.deepseek.com/chat/completions\n"); printf("模型: deepseek-chat\n"); printf("提示: %s\n", prompt); printf("状态码: %ld\n", http_code); if(res != CURLE_OK) { fprintf(stderr, "cURL错误: %s\n", curl_easy_strerror(res)); if(chunk.memory) free(chunk.memory); curl_global_cleanup(); return api_response; } // 检查HTTP状态码 if(http_code != 200) { fprintf(stderr, "API错误: HTTP %ld\n", http_code); if(chunk.memory) { printf("原始响应: %s\n", chunk.memory); cJSON *error_json = cJSON_Parse(chunk.memory); if(error_json) { cJSON *error_obj = cJSON_GetObjectItem(error_json, "error"); if(error_obj) { cJSON *message = cJSON_GetObjectItem(error_obj, "message"); cJSON *code = cJSON_GetObjectItem(error_obj, "code"); if(cJSON_IsString(message)) { fprintf(stderr, "错误信息: %s\n", message->valuestring); } if(cJSON_IsString(code)) { fprintf(stderr, "错误代码: %s\n", code->valuestring); } } cJSON_Delete(error_json); } } if(chunk.memory) free(chunk.memory); curl_global_cleanup(); return api_response; } curl_global_cleanup(); // 解析API响应 cJSON *response_json = cJSON_Parse(chunk.memory); if (!response_json) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr) fprintf(stderr, "JSON解析错误: %s\n", error_ptr); fprintf(stderr, "原始响应: %s\n", chunk.memory); free(chunk.memory); return api_response; } // 提取令牌使用信息 cJSON *usage = cJSON_GetObjectItem(response_json, "usage"); if (usage) { cJSON *prompt_tokens = cJSON_GetObjectItem(usage, "prompt_tokens"); cJSON *completion_tokens = cJSON_GetObjectItem(usage, "completion_tokens"); cJSON *total_tokens = cJSON_GetObjectItem(usage, "total_tokens"); if (cJSON_IsNumber(prompt_tokens)) api_response.prompt_tokens = prompt_tokens->valueint; if (cJSON_IsNumber(completion_tokens)) api_response.completion_tokens = completion_tokens->valueint; if (cJSON_IsNumber(total_tokens)) api_response.total_tokens = total_tokens->valueint; } // 提取回复内容 cJSON *choices = cJSON_GetObjectItem(response_json, "choices"); if (cJSON_IsArray(choices) && cJSON_GetArraySize(choices) > 0) { cJSON *first_choice = cJSON_GetArrayItem(choices, 0); cJSON *message = cJSON_GetObjectItem(first_choice, "message"); cJSON *content = cJSON_GetObjectItem(message, "content"); if (cJSON_IsString(content) && content->valuestring != NULL) { api_response.content = strdup(content->valuestring); } } cJSON_Delete(response_json); free(chunk.memory); return api_response; } // 显示令牌使用信息 void print_token_usage(int prompt_tokens, int completion_tokens, int total_tokens) { printf("\n--- 令牌使用 ---\n"); printf("提示令牌: %d\n", prompt_tokens); printf("完成令牌: %d\n", completion_tokens); printf("总令牌: %d\n", total_tokens); // 估算成本 double estimated_cost = total_tokens * 0.0000015; // $0.0015/1K tokens printf("估算成本: $%.6f\n", estimated_cost); } // ================== 会话管理函数 ================== // 创建新会话 DeepSeekSession* deepseek_create_session(const char *api_key) { DeepSeekSession *session = malloc(sizeof(DeepSeekSession)); if (!session) return NULL; // 修复类型不匹配问题 if (api_key) { session->api_key = strdup(api_key); } else { session->api_key = get_api_key(); } session->total_prompt_tokens = 0; session->total_completion_tokens = 0; session->total_all_tokens = 0; return session; } // 销毁会话 void deepseek_destroy_session(DeepSeekSession *session) { if (!session) return; free(session->api_key); free(session); } // 发送消息并获取回复 char* deepseek_send_message( DeepSeekSession *session, const char *message, int *prompt_tokens, // 返回本次提示令牌 int *completion_tokens, // 返回本次完成令牌 int *total_tokens // 返回本次总令牌 ) { if (!session || !message) return NULL; struct APIResponse response = call_deepseek(session->api_key, message); if (response.content) { // 更新会话令牌计数 session->total_prompt_tokens += response.prompt_tokens; session->total_completion_tokens += response.completion_tokens; session->total_all_tokens += response.total_tokens; // 设置输出参数 if (prompt_tokens) *prompt_tokens = response.prompt_tokens; if (completion_tokens) *completion_tokens = response.completion_tokens; if (total_tokens) *total_tokens = response.total_tokens; return response.content; } return NULL; } // 获取会话令牌统计 void deepseek_get_token_stats( DeepSeekSession *session, int *total_prompt, int *total_completion, int *total_all ) { if (!session) return; if (total_prompt) *total_prompt = session->total_prompt_tokens; if (total_completion) *total_completion = session->total_completion_tokens; if (total_all) *total_all = session->total_all_tokens; } void get_full_text_input(char *dest, size_t dest_size) { // 获取文本区域内容指针 const char *text = lv_textarea_get_text(ui_TextArea1); if (!text) { dest[0] = &#39;\0&#39;; return; } // 计算实际文本长度(包括空格) size_t text_len = strlen(text); // 安全复制文本(防止缓冲区溢出) size_t copy_len = text_len < dest_size - 1 ? text_len : dest_size - 1; strncpy(dest, text, copy_len); dest[copy_len] = &#39;\0&#39;; // 确保字符串终止 // 移除末尾的换行符(如果有) char *newline = strchr(dest, &#39;\n&#39;); if (newline) *newline = &#39;\0&#39;; printf("111111111111\n"); } // ================== 主函数 ================== int depmainlong(lv_event_t * e) // 创建会话 { lv_event_code_t event_code = lv_event_get_code(e); lv_obj_t * target = lv_event_get_target(e); if( event_code == LV_EVENT_LONG_PRESSED ) { // 创建会话 - 修复API密钥传递问题 // session = deepseek_create_session("sk-28b778879e5b4fd6b227d767812fd83d"); // if (!session) { // fprintf(stderr, "创建会话失败\n"); // return 1; // } printf("DeepSeek聊天客户端 (输入&#39;exit&#39;退出)\n"); printf("使用的API密钥: %.6s...\n", session->api_key); // if( !lv_obj_has_flag(ui_Keyboard1, LV_OBJ_FLAG_HIDDEN)) { // printf("\n你的问题: "); // // char input[1024]; // // 如何判定提交文本时机,以及如何控制循环 // /********************* // * @brief Construct a new if object // * @details 按键长按不起作用,起作用的是点击 // * 点击一次,进入一次,判断一次。 // * 而由于全局变量的设置,文本内容与对话的循环并无直接关系 // * 目前会出现段错误,猜测可能是由于内容缓冲区越界,因为还保留了上下文 // *************************************************/ // if( event_code == LV_EVENT_CLICKED ) // { // printf("inputlogo被长按!\n"); // printf("111111111111"); // } // if(1) // { // kbEntertext = lv_textarea_get_text(ui_TextArea1); // printf("提交内容: %s\n", kbEntertext); // } // // 使用这种方式需要处理循环的问题以及刷新。 // sscanf(kbEntertext, "%s", input); // printf("kbEntertext = %s\n", kbEntertext); // printf("input = %s\n", input); // // 移除换行符 // input[strcspn(input, "\n")] = 0; // // 线程阻塞原因,暂不使用循环while 以及相应的语句 // // if(strcmp(input, "exit") == 0) break; // // if(strlen(input) == 0) continue; // printf("正在查询DeepSeek API...\n"); // // int prompt_tokens, completion_tokens, total_tokens; // // char *response = deepseek_send_message // response = deepseek_send_message( // session, // input, // &prompt_tokens, // &completion_tokens, // &total_tokens // ); // if(response) { // // printf("\n--- DeepSeek回复 ---\n%s\n", response); // printf("\n--- DeepSeek回复 ---\n"); // sprintf(dpOut, "%s", response); // printf("%s\n", dpOut); // lv_label_set_text(ui_AILabel, dpOut); // // 显示令牌使用 // print_token_usage(prompt_tokens, completion_tokens, total_tokens); // free(response); // } else { // printf("获取回复时出错\n"); // } // } return 0; } } int depmaintalk(lv_event_t * e) // 会话 { lv_event_code_t event_code = lv_event_get_code(e); lv_obj_t * target = lv_event_get_target(e); if( event_code == LV_EVENT_CLICKED ) { if( !lv_obj_has_flag(ui_Keyboard1, LV_OBJ_FLAG_HIDDEN)) { printf("\n你的问题: "); // char input[1024]; printf("inputlogo被anxia!\n"); // kbEntertext = lv_textarea_get_text(ui_TextArea1); // input = lv_textarea_get_text(ui_TextArea1); get_full_text_input(input, sizeof(input)); // sscanf(lv_textarea_get_text(ui_TextArea1), "%s", input); printf("kbEntertext = %s\n", kbEntertext); printf("input = %s\n", input); if( strcmp(input, "exit") == 0 ) { // 清理资源 deepseek_destroy_session(session); printf("退出!\n"); return 0; } // 使用这个清理判断会卡住,不知道为什么 // if( strcmp(input, "exit") ) // { // // 清理资源 // deepseek_destroy_session(session); // return 0; // } printf("正在查询DeepSeek API...\n"); // char *response = deepseek_send_message response = deepseek_send_message( session, input, &prompt_tokens, &completion_tokens, &total_tokens ); if(response) { // printf("\n--- DeepSeek回复 ---\n%s\n", response); printf("\n--- DeepSeek回复 ---\n"); sprintf(dpOut, "%s", response); printf("%s\n", dpOut); lv_label_set_text(ui_AILabel, dpOut); // 显示令牌使用 print_token_usage(prompt_tokens, completion_tokens, total_tokens); free(response); } else { printf("获取回复时出错\n"); } } if( strcmp(input, "exit") == 0 ) { // 清理资源 deepseek_destroy_session(session); return 0; } } }
06-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值