Skip to content

Commit

Permalink
module esp8266_network to network: code.c
Browse files Browse the repository at this point in the history
  • Loading branch information
chris committed May 31, 2024
1 parent dfc32f0 commit debc1d7
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 446 deletions.
Binary file modified c_mX.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions code.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include "lcd_log.h"

#include "esp8266_driver.h"
#include "esp8266_network.h"
#include "network.h"
#include "app.h"

#include "Widget.h"
Expand Down Expand Up @@ -129,7 +129,7 @@ TASK(Task_ESP82266_RevResponse)
* Task used for FSM.
*/
TASK(Task_ESP8266_FSM) {
app_fsm_app();
network();
}

TASK(Task_Weather_Update) {
Expand Down
57 changes: 25 additions & 32 deletions inc/WidgetConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,30 @@
#include "stm32f4_discovery_lcd.h"


#define NUMWIDGETS 16

#define LOCATION_IMG 0
#define CITY_STR 1
#define WiFi_IMG 2
#define WiFi_AP_SET 3
#define DESCRIPTION_STR 4
#define TEMP_DEGREE_STR 5
#define FEELS_STR 6
#define LIKE_STR 7
#define FEELS_LIKE_VAL_STR 8
#define TEMP_DEGREE2_STR 9

#define OW01D_IMG 10

#define TEMP_SIGN_IMG 11
#define TEMP_DEC_IMG 12
#define TEMP_UNI_IMG 13
#define STATE_DEV_STR 14

#define REQ_TIME 15

#define SET_AP_ESP8266_EVNT 0x02
#define SPOTIFY_CONF_EVENT 0x01

// #define TIMEMODE 0x00
// #define TIMESETMODE 0x01
// #define ALARMMODE 0x02
// #define SWATCHMODE 0x04
// #define PLUS 0x10
// #define MINUS 0x20
//
enum WidgetID{
LOCATION_IMG,
CITY_STR,
WiFi_IMG,
WiFi_AP_SET,
DESCRIPTION_STR,
TEMP_DEGREE_STR,
FEELS_STR,
LIKE_STR,
FEELS_LIKE_VAL_STR,
TEMP_DEGREE2_STR,
OW01D_IMG,
TEMP_SIGN_IMG,
TEMP_DEC_IMG,
TEMP_UNI_IMG,
STATE_DEV_STR,
REQ_TIME,
SPOTIFY_TRACK_STR,
NUMWIDGETS,
};

#define SET_AP_ESP8266_EVNT 0x02
#define SPOTIFY_CONF_EVENT 0x01

// 0xb798 -> This is background color in a lazy way
#define APP_BACKGROUND_COLOR 0x7f12

Expand Down Expand Up @@ -75,6 +67,7 @@ void UI_writeWeatherFeelsLike(char *feels_like_val);
void UI_setWeatherIcon(unsigned short *iconID);
void UI_writeWeatherCurrTemp(char *curr_temp);
void UI_setTime(char *timezone, char *time);
void UI_set_track(char *track);

void UI_SettingsOn(void);
void UI_SettingsOff(void);
Expand Down
2 changes: 1 addition & 1 deletion inc/esp8266_link.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ESP8266_LINK_H
#define ESP8266_LINK_H

#include "esp8266_network.h"
#include "network.h"

uint16_t LUT_link_pullin(enum ESP8NetManagerState supers);
uint8_t app_http_process(enum ESP8NetManagerState supers, void (*callback)(uint8_t*, char*, void*), void *arg);
Expand Down
56 changes: 0 additions & 56 deletions inc/esp8266_network.h

This file was deleted.

2 changes: 1 addition & 1 deletion inc/esp8266_server.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ESP8266_SERVER_H
#define ESP8266_SERVER_H

#include "esp8266_network.h"
#include "network.h"

enum ESP8ServerState {
ESP8S_MULTI_CONN_AP,
Expand Down
2 changes: 1 addition & 1 deletion inc/esp8266_settings.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ESP8266_POWERUP_H
#define ESP8266_POWERUP_H

#include "esp8266_network.h"
#include "network.h"

enum ESP8PowerupState {
ESP8S_RESTART,
Expand Down
12 changes: 11 additions & 1 deletion inc/spotify.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ enum {
#define SPOTIFY_RTOKEN_SIZE 150
#define SPOTIFY_AUTH_CONTENT_SIZE 430

char spotify_get_token(char *http, char **tokens, uint16_t *sizes);
char spotify_get_token(char *http, char **vals, uint16_t *sizes);

/*** API ***/
#define SPOTIFY_API "\"api.spotify.com\",443"
Expand All @@ -86,4 +86,14 @@ char spotify_get_token(char *http, char **tokens, uint16_t *sizes);
"Authorization: Bearer %s\r\n"\
"Content-Length: 0\r\n\r\n"

enum {
iSPOTIFY_SONG,
iSPOTIFY_ARTIST,
SPOTIFY_TRACK_COUNT
};

char spotify_get_track(char *json, char **vals, uint16_t *sizes);



#endif
73 changes: 49 additions & 24 deletions src/app.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "esp8266_network.h"
#include "network.h"
#include "esp8266_settings.h"
#include "esp8266_server.h"
#include "esp8266_client.h"
Expand Down Expand Up @@ -96,9 +96,8 @@ void app_http_from_WebApp(uint8_t *success, char *http, void *arg) {
struct Socket *sock = (struct Socket *) arg;
SSIDnPSWD_t *snp = (SSIDnPSWD_t *) sock->arg;

MATCH(http, WRES_INDEX) {
// MAIN PAGE ("index") //
// Clean requested resource
MATCH(http, WRES_INDEX) {
// /index.html
*http = 0;

mkHTMLx(WSUPP_INDEX, HTTP_OK);
Expand All @@ -110,8 +109,7 @@ void app_http_from_WebApp(uint8_t *success, char *http, void *arg) {

}
MATCH(http, WRES_FAVICON) {
// Icon asked from the browser //
// Clean requested resources
// /favicon
memset(http, '\0', WRES_FAVICON_LEN);

mkHTMLx(WSUPP_NOT_FOUND, HTTP_NOT_FOUND);
Expand All @@ -122,8 +120,7 @@ void app_http_from_WebApp(uint8_t *success, char *http, void *arg) {

}
MATCH(http, WRES_SETWiFi) {
/* Submitted SSID and password for tne new Access Point to connect to */
// Clean posted resources
// /set_wifi
memset(http, '\0', WRES_SETWiFi_LEN);
char i = WRES_SETWiFi_LEN;

Expand Down Expand Up @@ -261,8 +258,8 @@ static void OWAPI_process_result(uint8_t *success, char *tmp, void *arg) {
}

static void spotify_token_processor(uint8_t *success, char *http, void *arg) {
char *tokens_ptr[2];
uint16_t sizes[2];
char *tokens_ptr[SPOTIFY_TOKEN_COUNT];
uint16_t sizes[SPOTIFY_TOKEN_COUNT];
char **token = (char **)arg;

if(spotify_get_token(http, tokens_ptr, sizes)) {
Expand All @@ -282,29 +279,44 @@ static void spotify_token_processor(uint8_t *success, char *http, void *arg) {
}

static void spotify_track_processor(uint8_t *success, char *http, void *arg) {
char *track_ptr[2];
uint16_t sizes[2];

if (esp8_status.http == HTTP_204) {
*success = 0;
return;
}

if(spotify_get_track(http, track_ptr, sizes)) {
*success = 1;
return;
}

*(track_ptr[1] + sizes[1]) = 0;
UI_set_track(track_ptr[1]);

*success = 0;
}


/******** Server Function *********/
enum AppServerState {
SERVER_CONF,
SERVER_RUNNING,
};

enum ServersID {
NET_CONF,
SPOTIFY_CONF,
WIFI_SUPPLICANT,
SPOTIFY_CODE,
};

void server_function(struct StateS *s, enum ServersID server_id) {
static struct Socket socket;
static SSIDnPSWD_t wifi_credentials;
static enum AppServerState server_state = SERVER_CONF;

if (server_id == NET_CONF) {

switch(server_id) {
case WIFI_SUPPLICANT:
switch(server_state) {
case SERVER_CONF:
socket.callback = &app_http_from_WebApp;
Expand All @@ -313,16 +325,20 @@ void server_function(struct StateS *s, enum ServersID server_id) {
esp8_status.wifi = WiFi_NO_CONNECTED;
break;
}


// case SERVER_RUNNING:
fsm_server(s, &socket);

if (wifi_credentials.size > 0 && SUBSTATE(*s->state) == ESP8S_LISTENING)
fsm_station_credentials(s, &wifi_credentials);
}
else if (server_id == SPOTIFY_CONF) {

break;

case SPOTIFY_CODE:
switch(server_state) {
case SERVER_CONF:
spotify_code = snprintf(spotify_auth_content, SPOTIFY_AUTH_CONTENT_SIZE, \
spotify_code = snprintf(spotify_auth_content,\
SPOTIFY_AUTH_CONTENT_SIZE,\
SPOTIFY_AUTH_CONTENT, ESP8266_IPv4.ip);

if (spotify_code < 0) {
Expand All @@ -336,12 +352,15 @@ void server_function(struct StateS *s, enum ServersID server_id) {
*(spotify_auth_content + SPOTIFY_AUTH_RANDOM_POS) = 0;
break;
}


// case SERVER_RUNNING:
fsm_server(s, &socket);

if(*(spotify_auth_content + SPOTIFY_AUTH_RANDOM_POS) > ' ' && \
SUBSTATE(*s->state) == ESP8S_LISTENING)
{
*s->nx_state = MKSTATE(ESP8SS_INITIAL_SETUP, ESP8S_RESTART);
}

}

Expand All @@ -350,6 +369,7 @@ void server_function(struct StateS *s, enum ServersID server_id) {

}


enum AppClientState {
CLIENT_CONF,
CLIENT_EXEC,
Expand Down Expand Up @@ -475,7 +495,8 @@ void client_function(struct StateS *state) {

fsm_client(state, &sock);

if(SUPERSTATE(*state->nx_state) == ESP8SS_READY && *spotify_token > ' ') {
if(SUPERSTATE(*state->nx_state) == ESP8SS_READY && \
*spotify_token > ' ') {
client_state = CLIENT_CONF;
client_id = SPOTIFY_PLAYER;
*state->nx_state = MKSTATE(ESP8SS_CLIENT, ESP8S_CONNECT_SSL);
Expand Down Expand Up @@ -522,14 +543,18 @@ void client_function(struct StateS *state) {

if (SUPERSTATE(*state->nx_state) == ESP8SS_READY) {
if (esp8_status.http == HTTP_401) {
client_state = CLIENT_CONF;
// When the Token is expired a renewal is asked
// using the refreshing token.
client_state = CLIENT_CONF;
spotify_code = snprintf(spotify_auth_content, SPOTIFY_AUTH_CONTENT_SIZE, \
spotify_code = snprintf(spotify_auth_content,\
SPOTIFY_AUTH_CONTENT_SIZE,\
SPOTIFY_REFRESH_CONTENT, spotify_rtoken);

if (spotify_code < 0 || spotify_code > SPOTIFY_AUTH_CONTENT_SIZE) {
memset(spotify_auth_content, 0, SPOTIFY_AUTH_CONTENT_SIZE);
if (spotify_code < 0 ||\
spotify_code > SPOTIFY_AUTH_CONTENT_SIZE)
{
memset(spotify_auth_content, 0,\
SPOTIFY_AUTH_CONTENT_SIZE);
client_id = LOCATION;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/esp8266_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>

#include "esp8266_driver.h"
#include "esp8266_network.h"
#include "network.h"
#include "esp8266_client.h"
#include "esp8266_link.h"
#include "esp8266_settings.h"
Expand Down
2 changes: 1 addition & 1 deletion src/esp8266_netstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "state.h"
#include "esp8266_driver.h"
#include "esp8266_network.h"
#include "network.h"
#include "esp8266_responses.h"
#include "esp8266_settings.h"
#include "esp8266_netstat.h"
Expand Down
Loading

0 comments on commit debc1d7

Please sign in to comment.