Skip to content

Commit

Permalink
Fix bugs and upload the web pages
Browse files Browse the repository at this point in the history
  • Loading branch information
subjectxbj committed Mar 26, 2018
1 parent 91989f3 commit bb16a47
Show file tree
Hide file tree
Showing 14 changed files with 861 additions and 61 deletions.
80 changes: 80 additions & 0 deletions AlexaClientSDKConfig_default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"authDelegate":{
// The Client Secret of the Product from developer.amazon.com
"clientSecret":"70c955eaa6b998bb3eff97f051b18a5f72b21d38e3ba102da1e83aafdd0efa1f",
// Unique device serial number. e.g. 123456
"deviceSerialNumber":"CXNK000ABCDE",
// Refresh Token populated by running AuthServer.py
"refreshToken":"",
// The Client ID of the Product from developer.amazon.com
"clientId":"amzn1.application-oa2-client.a308f3dc39a54349b970f19b13c86f5d",
// Product ID from developer.amazon.com
"productId":"GC4026E"
},
"alertsCapabilityAgent":{
// Path to Alerts database file. e.g. /home/ubuntu/Build/alerts.db
// Note: The directory specified must be valid.
// The database file (alerts.db) will be created by SampleApp, do not create it yourself.
// The database file should only be used for alerts (don't use it for other components of SDK)
"databaseFilePath":"",
// Path to default Alarm sound file. e.g. /home/ubuntu/alert_sounds/alarm_normal.mp3
// Note: The audio file must exist and be a valid file.
"alarmSoundFilePath":"",
// Path to short Alarm sound file. e.g. /home/ubuntu/alert_sounds/alarm_short.wav
// Note: The audio file must exist and be a valid file.
"alarmShortSoundFilePath":"",
// Path to default timer sound file. e.g. /home/ubuntu/alert_sounds/timer_normal.mp3
// Note: The audio file must exist and be a valid file.
"timerSoundFilePath":"",
// Path to short timer sound file. e.g. /home/ubuntu/alert_sounds/timer_short.wav
// Note: The audio file must exist and be a valid file.
"timerShortSoundFilePath":""
},
"settings":{
// Path to Settings database file. e.g. /home/ubuntu/Build/settings.db
// Note: The directory specified must be valid.
// The database file (settings.db) will be created by SampleApp, do not create it yourself.
// The database file should only be used for settings (don't use it for other components of SDK)
"databaseFilePath":"",
"defaultAVSClientSettings":{
// Default language for Alexa.
// See https://developer.amazon.com/docs/alexa-voice-service/settings.html#settingsupdated for valid values.
"locale":""
}
},
"certifiedSender":{
// Path to Certified Sender database file. e.g. /home/ubuntu/Build/certifiedsender.db
// Note: The directory specified must be valid.
// The database file (certifiedsender.db) will be created by SampleApp, do not create it yourself.
// The database file should only be used for certifiedSender (don't use it for other components of SDK)
"databaseFilePath":""
}
}


// Notes for logging
// The log levels are supported to debug when SampleApp is not working as expected.
// There are 14 levels of logging with DEBUG9 providing the highest level of logging and CRITICAL providing
// the lowest level of logging i.e. if DEBUG9 is specified while running the SampleApp, all the logs at DEBUG9 and
// below are displayed, whereas if CRITICAL is specified, only logs of CRITICAL are displayed.
// The 14 levels are:
// DEBUG9, DEBUG8, DEBUG7, DEBUG6, DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, DEBUG0, INFO, WARN, ERROR, CRTITICAL.

// To selectively see the logging for a particular module, you can specify logging level in this json file.
// Some examples are:
// To only see logs of level INFO and below for ACL and MediaPlayer modules,
// - grep for ACSDK_LOG_MODULE in source folder. Find the log module for ACL and MediaPlayer.
// - Put the following in json:

// "acl":{
// "logLevel":"INFO"
// },
// "mediaPlayer":{
// "logLevel":"INFO"
// }

// To enable DEBUG, build with cmake option -DCMAKE_BUILD_TYPE=DEBUG. By default it is built with RELEASE build.
// And run the SampleApp similar to the following command.
// e.g. TZ=UTC ./SampleApp /home/ubuntu/.../AlexaClientSDKConfig.json /home/ubuntu/KittAiModels/ DEBUG9"


18 changes: 14 additions & 4 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ json_object* getCleanConfig(char *filename) {
//printf(cJSON_Print(json));
//printf("\n");
}else{
printf("Failed to parse response\n");
printf("Failed to parse config\n");
return NULL;
}
return json;

Expand Down Expand Up @@ -123,11 +124,20 @@ char *get_config_param_value(json_object *config, char *name){
}

int update_config_param(json_object *config, char *name, char *value) {
json_object_object_del(config, name);
json_object *authDelegate = json_object_object_get(config, "authDelegate");
if(authDelegate == NULL) {
return -1;
}
if (strcmp(name, "refreshToken") == 0){
json_object_object_del(authDelegate, "refreshToken");

json_object_object_add(config, name, json_object_new_string(value));
json_object_object_add(authDelegate, "refreshToken", json_object_new_string(value));

return 0;
}

return 0;
return -1;

}


58 changes: 29 additions & 29 deletions lwa.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ char * clientSecret = "70c955eaa6b998bb3eff97f051b18a5f72b21d38e3ba102da1e83aafd
#endif
char * scope = "alexa:all";
char * responseType = "code";
char * urlencode(char *json_string)
static char * urlencode(char *json_string)
{
static char output[1024];
memset(output, 0, sizeof(output));
Expand All @@ -45,7 +45,7 @@ char * urlencode(char *json_string)
}


char* getRedirectUrl(){
static char* getRedirectUrl(json_object *config){
static char lwaUrl[1000];
char *scopeData;
json_object *alexa_all, *attr;
Expand All @@ -54,13 +54,13 @@ char* getRedirectUrl(){
#ifdef HARDCODE_CONFIG
json_object_object_add(alexa_all, "productID", json_object_new_string(productId));
#else
json_object_object_add(alexa_all, "productID",json_object_new_string(get_config_param_value(json_config, "productId")));
json_object_object_add(alexa_all, "productID",json_object_new_string(get_config_param_value(config, "productId")));
#endif
json_object_object_add(alexa_all, "productInstanceAttributes", attr=json_object_new_object());
#ifdef HARDCODE_CONFIG
json_object_object_add(attr, "deviceSerialNumber", json_object_new_string(deviceSerialNumber));
#else
json_object_object_add(attr, "deviceSerialNumber", json_object_new_string(get_config_param_value(json_config, "deviceSerialNumber")));
json_object_object_add(attr, "deviceSerialNumber", json_object_new_string(get_config_param_value(config, "deviceSerialNumber")));
#endif
scopeData = json_object_to_json_string(root);
json_object_put(root);
Expand All @@ -73,7 +73,7 @@ char* getRedirectUrl(){
#ifdef HARDCODE_CONFIG
sprintf(param, "client_id=%s", clientId);
#else
sprintf(param, "client_id=%s", get_config_param_value(json_config, "clientId"));
sprintf(param, "client_id=%s", get_config_param_value(config, "clientId"));
#endif
printf("param:%s\n",param);
strcat(lwaUrl, param);
Expand Down Expand Up @@ -103,7 +103,7 @@ char* getRedirectUrl(){


}
int parseResponse(char *response) {
static int parseResponse(char *response, json_object *config) {
json_object *json = NULL;
int ret;
json = json_tokener_parse(response);
Expand All @@ -113,15 +113,15 @@ int parseResponse(char *response) {
char *refreshToken = json_object_get_string(refreshTokenObject);
if(refreshToken) {
printf("\n==============>Get Refresh Token: [%s]\n", refreshToken);
ret = update_config_param(json_config, "refreshToken", refreshToken);
ret = update_config_param(config, "refreshToken", refreshToken);
if (ret != 0){
printf("\nERROR: Failed to update refresh token\n");
json_object_put(json);
return -1;
}
ret = writeConfig(json_config, config_out_path);
ret = writeConfig(config, config_path);
if (ret != 0){
printf("\nERROR: Failed to write config to [%s]\n", config_out_path);
printf("\nERROR: Failed to write config to [%s]\n", config_path);
json_object_put(json);
return -1;
}
Expand All @@ -139,14 +139,14 @@ int parseResponse(char *response) {
}
}
char resp[10240]={0};
size_t write_data(void* buffer, size_t size, size_t nmemb, void* response) {
static size_t write_data(void* buffer, size_t size, size_t nmemb, void* response) {
int len = size*nmemb;
memset(resp, 0, sizeof(resp));
memcpy(response, buffer, len);
return len;
}

int requestRefreshToken(char *code) {
static int requestRefreshToken(char *code, json_object *config) {
CURL *curl;
CURLcode res;
int ret;
Expand All @@ -170,12 +170,12 @@ int requestRefreshToken(char *code) {
#ifdef HARDCODE_CONFIG
clientId,
#else
get_config_param_value(json_config, "clientId"),
get_config_param_value(config, "clientId"),
#endif
#ifdef HARDCODE_CONFIG
clientSecret,
#else
get_config_param_value(json_config, "clientSecret"),
get_config_param_value(config, "clientSecret"),
#endif
urlencode(redirect_uri)
);
Expand Down Expand Up @@ -205,7 +205,7 @@ int requestRefreshToken(char *code) {
printf("OK\n");
}
printf("\n==============>Get RESPONSE: %s\n", resp);
ret = parseResponse(resp);
ret = parseResponse(resp, config);
if (ret != 0){
printf("ERROR: parseResponse failed\n");
curl_easy_cleanup(curl);
Expand All @@ -226,9 +226,20 @@ int requestRefreshToken(char *code) {
}
}

int handleUserRequest(int client) {
static int returnResult(int client, int status, char *info) {
char content[1024]={0};
write(client, "HTTP/1.0 200\n",13);
write(client, "\n", 1);
if (status == 0){
sprintf(content, "<html><head><title>AVS LWA Success</title></head><body>Success</body></html>\n");
}else{
sprintf(content, "<html><head><title>AVS LWA Failure</title></head><body>Failed.%s</body></html>\n", info);
}
write(client, content, strlen(content));
}
int handleUserRequest(int client, json_object *config) {
printf("\n==============>Receive User Request");
char *redirectUrl = getRedirectUrl();
char *redirectUrl = getRedirectUrl(config);
if (redirectUrl == NULL) {
return -1;
}
Expand All @@ -240,18 +251,7 @@ int handleUserRequest(int client) {
write(client, "\n", 1);
return 0;
}
int returnResult(int client, int status, char *info) {
char content[1024]={0};
write(client, "HTTP/1.0 200\n",13);
write(client, "\n", 1);
if (status == 0){
sprintf(content, "<html><head><title>AVS LWA Success</title></head><body>Success</body></html>\n");
}else{
sprintf(content, "<html><head><title>AVS LWA Failure</title></head><body>Failed.%s</body></html>\n", info);
}
write(client, content, strlen(content));
}
int handleAuthCodeGrant(int client, char *request) {
int handleAuthCodeGrant(int client, char *request, json_object *config) {
char *tag, *code;
int ret;
printf("\n==============>Receive authresponse");
Expand All @@ -265,7 +265,7 @@ int handleAuthCodeGrant(int client, char *request) {
returnResult(client, -1, "Failed to extract auth code");
return -1;
}
ret = requestRefreshToken(code);
ret = requestRefreshToken(code, config);
if (ret != 0){
printf("\nERROR: Failed to get refresh token\n");
returnResult(client, -1, "Failed to get refresh token");
Expand Down
6 changes: 3 additions & 3 deletions lwa.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern char config_out_path[256];
extern char config_path[256];
char redirect_uri[256];
int handleUserRequest(int client);
int handleAuthCodeGrant(int client, char *request);
int handleUserRequest(int client, json_object *config);
int handleAuthCodeGrant(int client, char *request, json_object *config);
Loading

0 comments on commit bb16a47

Please sign in to comment.