forked from nkolban/esp32-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kolban
committed
Nov 22, 2016
1 parent
e00f07a
commit 5dcef5f
Showing
7 changed files
with
391 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#include <stdlib.h> | ||
|
||
#include "c_list.h" | ||
|
||
/** | ||
* A list is an ordered set of entries. We have the following primitives: | ||
* * list_create() - Create an empty list. The return is the list pointer. | ||
* * list_delete() - Delete the list and optionally free all its entries. | ||
* * list_first() - Return the first item in the list. | ||
* * list_insert() - Add an item to the end of the list. | ||
* * list_insert_after() - Add an item to the list after a given entry. | ||
* * list_insert_before() - Add an item to the list before a given entry. | ||
* * list_next() - Get the next item in the list. | ||
* * list_remove() - Remove a specific item from the list. | ||
* * list_removeByValue() - Find the first element in the list with a matching value and remove it. | ||
*/ | ||
|
||
/** | ||
* Create a new list. | ||
*/ | ||
list_t *list_createList() { | ||
list_t *pList = malloc(sizeof(list_t)); | ||
pList->next = NULL; | ||
pList->prev = NULL; | ||
pList->value = NULL; | ||
return pList; | ||
} // list_createList | ||
|
||
/** | ||
* Delete a list. | ||
*/ | ||
void list_deleteList(list_t *pList, int withFree) { | ||
list_t *pNext; | ||
while(pList != NULL) { | ||
pNext = pList->next; | ||
if (withFree) { | ||
free(pList->value); | ||
} | ||
free(pList); | ||
pList = pNext; | ||
} | ||
} // list_deleteList | ||
|
||
/** | ||
* Insert a new item at the end of the list. | ||
*[A] -> [endOLD] ------> [A] -> [endOLD] -> [X] | ||
* | ||
*/ | ||
void list_insert(list_t *pList, void *value) { | ||
while(pList->next != NULL) { | ||
pList = pList->next; | ||
} | ||
list_insert_after(pList, value); | ||
} // list_insert | ||
|
||
/** | ||
* [pEntry] -> [B] ------> [pEntry] -> [X] -> [B] | ||
* | ||
*/ | ||
void list_insert_after(list_t *pEntry, void *value) { | ||
list_t *pNew = malloc(sizeof(list_t)); | ||
pNew->next = pEntry->next; | ||
pNew->prev = pEntry; | ||
pNew->value = value; | ||
|
||
// Order IS important here. | ||
if (pEntry->next != NULL) { | ||
pEntry->next->prev = pNew; | ||
} | ||
pEntry->next = pNew; | ||
} // list_insert_after | ||
|
||
/** | ||
* [A] -> [pEntry] ------> [A] -> [X] -> [pEntry] | ||
* | ||
*/ | ||
void list_insert_before(list_t *pEntry, void *value) { | ||
// Can't insert before the list itself. | ||
if (pEntry->prev == NULL) { | ||
return; | ||
} | ||
list_t *pNew = malloc(sizeof(list_t)); | ||
pNew->next = pEntry; | ||
pNew->prev = pEntry->prev; | ||
pNew->value = value; | ||
|
||
// Order IS important here. | ||
pEntry->prev->next = pNew; | ||
pEntry->prev = pNew; | ||
} // list_insert_before | ||
|
||
/** | ||
* Remove an item from the list. | ||
*/ | ||
void list_remove(list_t *pList, list_t *pEntry, int withFree) { | ||
while(pList != NULL && pList->next != pEntry) { | ||
pList = pList->next; | ||
} | ||
if (pList == NULL) { | ||
return; | ||
} | ||
pList->next = pEntry->next; | ||
if (pEntry->next != NULL) { | ||
pEntry->next->prev = pList; | ||
} | ||
if (withFree) { | ||
free(pEntry->value); | ||
} | ||
free(pEntry); | ||
} // list_delete | ||
|
||
/** | ||
* Delete a list entry by value. | ||
*/ | ||
void list_removeByValue(list_t *pList, void *value, int withFree) { | ||
list_t *pNext = pList->next; | ||
while(pNext != NULL) { | ||
if (pNext->value == value) { | ||
list_delete(pList, pNext, withFree); | ||
return; | ||
} | ||
} // End while | ||
} // list_deleteByValue | ||
|
||
/** | ||
* Get the next item in a list. | ||
*/ | ||
list_t *list_next(list_t *pList) { | ||
if (pList == NULL) { | ||
return NULL; | ||
} | ||
return (pList->next); | ||
} // list_next | ||
|
||
|
||
list_t *list_first(list_t *pList) { | ||
return pList->next; | ||
} // list_first | ||
|
||
void *list_get_value(list_t *pList) { | ||
return pList->value; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* c_utils.h | ||
* | ||
* Created on: Nov 15, 2016 | ||
* Author: kolban | ||
*/ | ||
|
||
#ifndef COMPONENTS_C_LIST_H_ | ||
#define COMPONENTS_C_LIST_H_ | ||
|
||
typedef struct _list_t { | ||
void *value; | ||
struct _list_t *next; | ||
struct _list_t *prev; | ||
} list_t; | ||
|
||
list_t *list_createList(); | ||
void list_deleteList(list_t *pList, int withFree); | ||
void list_insert(list_t *pList, void *value); | ||
void list_remove(list_t *pList, list_t *pEntry, int withFree); | ||
list_t *list_next(list_t *pList); | ||
void list_removeByValue(list_t *pList, void *value, int withFree); | ||
list_t *list_first(list_t *pList); | ||
void list_insert_after(list_t *pEntry, void *value); | ||
void list_insert_before(list_t *pEntry, void *value); | ||
void *list_get_value(list_t *pList); | ||
#endif /* COMPONENTS_C_LIST_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#include "freertos/FreeRTOS.h" | ||
#include "esp_wifi.h" | ||
#include "esp_system.h" | ||
#include "esp_event.h" | ||
#include "esp_event_loop.h" | ||
#include "nvs_flash.h" | ||
#include "driver/gpio.h" | ||
#include "esp_log.h" | ||
#include "curl/curl.h" | ||
|
||
#define SSID "RASPI3" | ||
#define PASSWORD "password" | ||
|
||
/* | ||
#define SSID "guest" | ||
#define PASSWORD "kolbanguest" | ||
*/ | ||
|
||
static char tag[] = "main"; | ||
|
||
static size_t writeData(void *buffer, size_t size, size_t nmemb, void *userp) { | ||
ESP_LOGI(tag, "writeData called: buffer=0x%lx, size=%d, nmemb=%d", (unsigned long)buffer, size, nmemb); | ||
ESP_LOGI(tag, "data>> %.*s", size*nmemb, (char *)buffer); | ||
return size * nmemb; | ||
} | ||
|
||
void testCurl(void *taskData) { | ||
CURL *handle; | ||
CURLcode res; | ||
|
||
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); | ||
ESP_LOGI(tag, "Curl version info"); | ||
ESP_LOGI(tag, "version: %s - %d", data->version, data->version_num); | ||
ESP_LOGI(tag, "host: %s", data->host); | ||
if (data->features & CURL_VERSION_IPV6) { | ||
ESP_LOGI(tag, "- IP V6 supported"); | ||
} else { | ||
ESP_LOGI(tag, "- IP V6 NOT supported"); | ||
} | ||
if (data->features & CURL_VERSION_SSL) { | ||
ESP_LOGI(tag, "- SSL supported"); | ||
} else { | ||
ESP_LOGI(tag, "- SSL NOT supported"); | ||
} | ||
if (data->features & CURL_VERSION_LIBZ) { | ||
ESP_LOGI(tag, "- LIBZ supported"); | ||
} else { | ||
ESP_LOGI(tag, "- LIBZ NOT supported"); | ||
} | ||
if (data->features & CURL_VERSION_NTLM) { | ||
ESP_LOGI(tag, "- NTLM supported"); | ||
} else { | ||
ESP_LOGI(tag, "- NTLM NOT supported"); | ||
} | ||
if (data->features & CURL_VERSION_DEBUG) { | ||
ESP_LOGI(tag, "- DEBUG supported"); | ||
} else { | ||
ESP_LOGI(tag, "- DEBUG NOT supported"); | ||
} | ||
if (data->features & CURL_VERSION_UNIX_SOCKETS) { | ||
ESP_LOGI(tag, "- UNIX sockets supported"); | ||
} else { | ||
ESP_LOGI(tag, "- UNIX sockets NOT supported"); | ||
} | ||
ESP_LOGI(tag, "Protocols:"); | ||
int i=0; | ||
while(data->protocols[i] != NULL) { | ||
ESP_LOGI(tag, "- %s", data->protocols[i]); | ||
i++; | ||
} | ||
|
||
|
||
|
||
// Create a curl handle | ||
handle = curl_easy_init(); | ||
if (handle == NULL) { | ||
ESP_LOGI(tag, "Failed to create a curl handle"); | ||
vTaskDelete(NULL); | ||
return; | ||
} | ||
|
||
ESP_LOGI(tag, "Created a curl handle ..."); | ||
|
||
//char *url = "http://example.com"; | ||
char *url = "ftp://pi:[email protected]/.profile"; | ||
curl_easy_setopt(handle, CURLOPT_URL, url); | ||
/* example.com is redirected, so we tell libcurl to follow redirection */ | ||
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); | ||
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writeData); | ||
/* Perform the request, res will get the return code */ | ||
res = curl_easy_perform(handle); | ||
if (res != CURLE_OK) { | ||
ESP_LOGI(tag, "curl_easy_perform failed: %s", curl_easy_strerror(res)); | ||
} else { | ||
ESP_LOGI(tag, "curl_easy_perform() completed without incident."); | ||
} | ||
curl_easy_cleanup(handle); | ||
vTaskDelete(NULL); | ||
} // End of testCurl | ||
|
||
|
||
esp_err_t wifi_event_handler(void *ctx, system_event_t *event) | ||
{ | ||
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) { | ||
ESP_LOGI(tag, "Got an IP ... ready to go!"); | ||
xTaskCreatePinnedToCore(&testCurl, "testCurl", 10000, NULL, 5, NULL, 0); | ||
return ESP_OK; | ||
} | ||
return ESP_OK; | ||
} | ||
|
||
int app_main(void) | ||
{ | ||
nvs_flash_init(); | ||
system_init(); | ||
tcpip_adapter_init(); | ||
ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) ); | ||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | ||
ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); | ||
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); | ||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); | ||
wifi_config_t sta_config = { | ||
.sta = { | ||
.ssid = SSID, | ||
.password = PASSWORD, | ||
.bssid_set = 0 | ||
} | ||
}; | ||
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) ); | ||
ESP_ERROR_CHECK( esp_wifi_start() ); | ||
ESP_ERROR_CHECK( esp_wifi_connect() ); | ||
ESP_LOGI(tag, "Registering test vfs"); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <esp_err.h> | ||
|
||
char *espToString(esp_err_t value) { | ||
switch(value) { | ||
case ESP_OK: | ||
return "OK"; | ||
case ESP_FAIL: | ||
return "Fail"; | ||
case ESP_ERR_NO_MEM: | ||
return "No memory"; | ||
case ESP_ERR_INVALID_ARG: | ||
return "Invalid argument"; | ||
case ESP_ERR_INVALID_SIZE: | ||
return "Invalid state"; | ||
case ESP_ERR_INVALID_STATE: | ||
return "Invalid state"; | ||
case ESP_ERR_NOT_FOUND: | ||
return "Not found"; | ||
case ESP_ERR_NOT_SUPPORTED: | ||
return "Not supported"; | ||
case ESP_ERR_TIMEOUT: | ||
return "Timeout"; | ||
} | ||
if (value >= ESP_ERR_WIFI_BASE) { | ||
return "WiFi error"; | ||
} | ||
return "Unknown error"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.