FORGOT YOUR DETAILS?

CREATE ACCOUNT

Unison Help

  1. Unison Kernel
    1. Pthreads
      1. pthread_create()
      2. pthread_exit()
      3. pthread_self()
      4. pthread_equal()
      5. pthread_join()
      6. pthread_detach()
      7. pthread_setschedparam()
      8. pthread_getschedparam()
      9. pthread_attr_init()
      10. pthread_attr_destroy()
      11. pthread_attr_setstackaddr()
      12. pthread_attr_getstackaddr()
      13. pthread_attr_setstacksize()
      14. pthread_attr_getstacksize()
      15. pthread_attr_setschedparam()
      16. pthread_attr_getschedparam()
      17. pthread_attr_setdetachstate()
      18. pthread_attr_getdetachstate()
      19. pthread_stackinfo()
      20. pthread_setprio()
      21. pthread_getprio()
      22. sched_get_priority_max()
      23. sched_get_priority_min()
      24. sched_yield()
    2. Pthread Cancellation
      1. pthread_cleanup_pop()
      2. pthread_cleanup_push()
      3. pthread_cancel()
      4. pthread_setcanceltype()
      5. pthread_setcancelstate()
      6. pthread_testcancel()
    3. Mutex
      1. pthread_mutex_init()
      2. pthread_mutex_destroy()
      3. pthread_mutex_lock()
      4. pthread_mutex_trylock()
      5. pthread_mutex_unlock()
    4. Semaphores
      1. sem_open()
      2. sem_close()
      3. sem_unlink()
      4. sem_init()
      5. sem_destroy()
      6. sem_wait()
      7. sem_trywait()
      8. sem_timedwait()
      9. sem_post()
      10. sem_getvalue()
    5. Message Queues
      1. mq_open()
      2. mq_close()
      3. mq_unlink()
      4. mq_send()
      5. mq_receive()
      6. mq_notify()
      7. mq_setattr()
      8. mq_getattr()
      9. mq_timedreceive()
      10. mq_timedsend()
    6. Conditional Variables
      1. pthread_cond_init()
      2. pthread_cond_destroy()
      3. pthread_cond_wait()
      4. pthread_cond_timedwait()
      5. pthread_cond_signal()
      6. pthread_cond_broadcast()
      7. pthread_condattr_init()
      8. pthread_condattr_destroy()
    7. Barriers
      1. pthread_barrier_init()
      2. pthread_barrier_destroy()
      3. pthread_barrier_wait()
    8. Timers
      1. timer_create()
      2. timer_delete()
      3. timer_settime()
      4. timer_gettime()
      5. timer_getoverrun()
      6. timer_tick()
      7. nanosleep()
    9. Clocks
      1. time()
      2. uptime()
      3. sleep()
      4. clock_settime()
      5. clock_gettime()
      6. clock_getres()
      7. clock_init()
    10. Memory Allocation
      1. POSIX.1
        1. malloc()
        2. free()
      2. Variable Length (Pools)
        1. pool_create()
        2. pool_destroy()
        3. pool_alloc()
        4. pool_free()
      3. Fixed Length (Partitions)
        1. pt_create()
        2. pt_destroy()
        3. pt_getblock()
        4. pt_freeblock()
    11. Rendezvous
      1. mr_init()
      2. mr_send()
      3. mr_receive()
      4. mr_reply()
      5. mr_sigrecv()
      6. mr_sigpost()
    12. Interrupts
      1. interrupts
      2. i_disable()
      3. i_restore()
    13. Directory Services
      1. dir_register()
      2. dir_deregister()
      3. dir_lookup()
      4. dir_lookup_string()
    14. Miscellaneous
      1. checkIstack()
      2. NanoStart() or DSPexec_Start()
      3. _isrStackFill
      4. Kernel Scaling
      5. kfatal()
      6. kalloc()
      7. kfree()
      8. mpu or mmu
      9. pthreadStackFill
      10. thread_numb()
      11. thread_utilization_start()
      12. thread_utilization_stop()
      13. xprintf()
      14. xputs()
      15. xputchar()
  2. Unison I/O Library
    1. accept()
    2. bind()
    3. chmod()
    4. close()
    5. connect()
    6. creat()
    7. fstat()
    8. getpeername()
    9. getsockname()
    10. getsockopt()
    11. ioctl()
    12. link()
    13. listen()
    14. lseek()
    15. mkdir()
    16. mkfs()
    17. mount()
    18. open()
    19. read()
    20. recv()
    21. recvfrom()
    22. rename()
    23. renameat()
    24. rmdir()
    25. select()
    26. send()
    27. sendto()
    28. setsockopt()
    29. shutdown()
    30. socket()
    31. stat()
    32. sync()
    33. umount()
    34. unlink()
    35. write()
  3. Unison STDIO Library
    1. STDIO Library Calls
      1. clearerr()
      2. dprintf()
      3. fclose()
      4. fdopen()
      5. feof()
      6. ferror()
      7. fileno()
      8. fflush()
      9. fgetc()
      10. fgetpos()
      11. fgets()
      12. fopen()
      13. fprintf()
      14. fputc()
      15. fputs()
      16. fread()
      17. freopen()
      18. fscanf()
      19. fseek()
      20. fseeko()
      21. fsetpos()
      22. ftell()
      23. ftello()
      24. fwrite()
      25. getc()
      26. getc_unlocked()
      27. getchar()
      28. getchar_unlocked()
      29. getdelim()
      30. getline()
      31. gets()
      32. get_stderr_ptr()
      33. get_stdin_ptr()
      34. get_stdout_ptr()
      35. noperprintf()
      36. perprintf()
      37. perror()
      38. posix_compat()
      39. printf()
      40. putc()
      41. putc_unlocked()
      42. putchar()
      43. putchar_unlocked()
      44. puts()
      45. remove()
      46. rewind()
      47. scanf()
      48. setbuf()
      49. setvbuf()
      50. snprintf()
      51. sprintf()
      52. sscanf()
      53. stderr_init()
      54. stderr_close()
      55. stdin_init()
      56. stdin_close()
      57. stdout_init()
      58. stdout_close()
      59. vdprintf()
      60. vscanf()
      61. vsscanf()
      62. vfscanf()
      63. vprintf()
      64. vsnprintf()
      65. vsprintf()
      66. vfprintf()
      67. ungetc()
    2. Do-nothing Stubs
      1. ctermid()
      2. flockfile()
      3. fmemopen()
      4. ftrylockfile()
      5. open_memstream()
      6. pclose()
      7. popen()
      8. tempnam()
      9. tmpfile()
      10. tmpnam()
  4. Unison LIBC Library
    1. LIBC Library Calls
      1. assert()
      2. realloc()
      3. strcasecmp()
      4. strdup()
      5. strncasecmp()
      6. strftime()
    2. Do-nothing Stubs
      1. abort()
      2. execve()
      3. exit()
      4. _Exit()
      5. fork()
      6. getpid()
      7. isatty()
      8. kill()
      9. sbrk()
      10. times()
      11. wait()
    3. Do-nothing Wide-character Stubs
      1. <wchar.h>
      2. <wctype.h>
  5. Unison I/O Servers
    1. File Servers
      1. Multimedia File Server - fsys
      2. FAT File System - fatfs
      3. NAND File Server - nandfsys
      4. NOR File Server - norfsys
      5. Network File Server - nfs
  6. Graphics, Camera, Video, Audio
    1. Vendor Graphics
    2. Prism++ Graphics
    3. ADPCM Services - adpcmd
    4. Camera
  7. Network Protocols
    1. TCP and UDP Server - tcpd
      1. IPv4 only server
      2. IPv4/IPv6 server
    2. DHCP Client Service - dhcp client
    3. DHCP Server - dhcpd
    4. Telnet Server - telnetd
    5. Tiny FTP Server - tftpd
    6. Point to Point - pppd
    7. Network Translation - NAT with PAT
    8. Firewall
      1. Packet filter: pf
      2. Packet filter control: pfctl
      3. Fitler rules: pf.filtering
      4. Translation rules: pf.nat
    9. Tiny HTTP Server - thttpd
    10. Tiny HTTP Server with TLS
    11. POP3 Server
    12. Simple Mail Transfer Protocol Services (SMTP)
    13. Bootp Protocol
    14. File Transfer Protocol Server (FTP)
    15. File Transfer Client Services
    16. RPC / XDR
    17. DNS Client
    18. HTTP/HTTPS Client
    19. REST Client
    20. AutoIP Service - autoip client
    21. mDNS server - mdnsd
    22. SNTP Client
    23. SNMP Agent - Snmpd server
    24. SSL/TLS library
    25. SSH server
    26. IP security
      1. IPsec description
      2. IPsec administration: ipsecadm
      3. Virtual Private Network: VPN
    27. Power Control
      1. Motor and Motion Control Servers
      2. PWM, Encoders
    28. Serial I/O
      1. Asynchronous Serial I/O Server - ttyserver
      2. CAN Server - cand
      3. I2C Server - i2cd
      4. I2S Server - i2sd
    29. System Services
      1. Power Management Servers
      2. Login Service - login_services
      3. XML
      4. POSIX Shell and Login Service - posh
    30. Universal Serial Bus (USB)
      1. USB Server
      2. USB Device Server
      3. USB Embedded Host Server
    31. Wireless
      1. Wireless Servers and Drivers
      2. 802.15.4 Radio Servers
      3. TCP/v6 with 6loWPAN
      4. ZigBee
      5. BlueTooth Server
      6. 802.11 Wi-Fi
      7. GPRS, UHF and GPS Radio Servers
    32. Remedy Tools for Unison
      1. Remedy Data Logging and Event Display Tools
      2. Remedy Diagnostics
      3. Remedy Flash Downloader/Bootloader
      4. Remedy Power On Self Test - POST
      5. Remedy OS Object Viewer
      6. Remedy Remote Control Tools

7.18.HTTP/HTTPS Client #

NAME

HTTP/HTTPS Client

SYNOPSIS

#include <thttpc.h>

int http_client_get (http_get_param *http_param, char *result);

int http_client_post (http_post_param *http_param, int connect_timeout_ms, int total_timeout_ms, int *httpreturncode, char *response_data, int response_max_length);

int https_client_get (http_get_param *http_param, char *result);

DESCRIPTION

HTTP/HTTPS client component allows user to use general commands for downloading and uploading files (HTTS only downloading). This is a HTTP GET and HTTP POST commands. The functions which realize that commands are represented below.

Functions description:

    • http_client_get – this fuction “gets” the separate file from HTTP server.
    • https_client_get – this fuction “gets” the separate file from HTTPS server using SSL/TLS protocols.

Parameters:

      • http_param – setup structure containing necessary settings for remote HTTP server.
      • result – pointer to space where function places resulting error code.

Possible error codes in result parameter:

HTTPC_ENOMEM
Returned if there is no free memory for the operation.
HTTPC_GENERAL_TCP_ERROR
Returned if TCP server error occurred.
HTTPC_HOST_NOT_ANSWER
Returned if host did not answered during the timeout period.
HTTPC_FILE_NOT_FOUND
Returned if file not found on the HTTP server.
HTTPC_DOWNLOAD_SIZE_ERROR
Returned if size of downloaded file differs from what we was waiting.
HTTPC_ERROR_FILE_WRITE
Returned if an error in write to file occurred.
HTTPC_TIMEOUT_DOWNLOAD
Returned if select() results in timeout.

“GET” setup structure:

typedef struct {
	const char	*host_name;
	const char	*host_ipaddr;
	const char	*host_path_file;
	int		host_port;			
	int		fd;
	int		max_file_size;
} http_get_param;
typedef struct {
	const char 	*host_name;
	const char 	*host_ipaddr;
	const char 	*host_path_file;
	SSL_params  SSL_param;
	int 	host_port;			
	int		fd;
	int		max_file_size;
} https_get_param;
typedef struct {
#ifndef SSL_CERT_FMT_NONE
	#define	SSL_CERT_FMT_NONE	0
	#define	SSL_CERT_FMT_PKCS8	1
	#define	SSL_CERT_FMT_PKCS12	2
#endif
	char	SSL_cert_format;
	union {
		struct{
			char	*SSL_sert;
			char	*SSL_key;
			char	*SSL_passwd;
		} file;
		struct {
			char	*SSL_key;
			int 	SSL_key_len;
			char	*SSL_passwd;

			char	*SSL_target_ss_cert;
			int 	SSL_target_ss_cert_len;
			char	*SSL_host_ss_cert;
			int 	SSL_host_ss_cert_len;
		} mem;
	} storage;
	enum {
		file_storage,
		memory_storage
	} storage_format;
} SSL_params;

 

    • http_client_post – this function “puts” the separate file onto remote HTTP server.

Parameters:

      • http_param – setup structure containing necessary settings for remote HTTP server.
      • connect_timeout_ms – connection timeout in milliseconds.
      • total_timeout_ms – general timeout in milliseconds for POST operation.
      • httpreturncode – pointer to specific return code from HTTP server.
      • response_data – pointer to space containing responce data from server.
      • response_max_length – value of response data size.
      • SSL_param – paraeters for HTTPS client
        • SSL_cert_format – SSL certificate format
        • storage_format – way of presenting the certificate (file_storage – sertificate stored in a files on diks; memory_storage – sertificate store as array in memory)
        • SSL_sert – path to SSL sertificate file
        • SSL_key -path to SSL key file
        • SSL_passwd – path to SSL key file
        • SSL_key – pointer to SSL key array
        • SSL_key_len – size of SSL key
        • SSL_passwd – pointer to SSL password array
        • SSL_target_ss_cert – pointer to array of target SelfSigned sertificate
        • SSL_target_ss_cert_len – size of array of target SelfSigned sertificate
        • SSL_host_ss_cert – pointer to array of host SelfSigned sertificate
        • SSL_host_ss_cert_len – of array of target SelfSigned sertificate

Error codes returning by http_client_post function:

HTTPC_OK
Returned if function work finished successfully.
HTTPC_ENOMEM
Returned if there is no free memory for the operation.
HTTPC_GENERAL_TCP_ERROR
Returned if TCP server error occurred.
HTTPC_HOST_NOT_ANSWER
Returned if host did not answered during the timeout period.
HTTPC_ERROR_FILE_READ
Returned if an error in read from file occurred.
HTTPC_TIMEOUT_UPLOAD
Returned if file uploading results in timeout.
HTTPC_TIMEOUT_CONNECT
Returned if connection to server (repeatable calls of connect()) results in timeout.
HTTPC_ERROR_DATA_SEND
Returned if data send to server was unsuccessfull.
HTTPC_ERROR_ANSWER
Returned if server answered with an error.

“POST” setup structure:

typedef struct {
	const char 	*host_name;
	const char 	*host_ipaddr;
	const char 	*host_path_file;
	const char 	*file_name;
	int 		host_port;			
	int		fd;
	int		file_size;
} http_post_param;

 

HTTP/HTTPS Client component works together with file system components. It may be FSYS or FATFS.

 

SPECIFIC OPTIONS (Compile time)

Option Default Description Values
SOC_SEND_TIMEOUT 40 Timeout value for sending to socket Decimal
BUFSIZE 1024(POST), 512(GET) Size of http working buffer Decimal

 

EXAMPLE

Example for HTTP GET function:

	int ret;
	char result;
	http_get_param http_param;

	memset(&http_param, 0, sizeof(http_get_param));

	// Example  Download file U41-51compare.pdf from site www.rowebots.com	
	//Download Link: "http://rowebots.com/articles/U41-51compare.pdf"

	if(( http_param.fd = open(FATFS_MOUNT"/U41-51compare.pdf", O_CREAT | O_RDWR, 0)) == -1)
	{
		xprintf("files opened  errno = %d\n", errno);
	}
	lseek( http_param.fd, 0 , SEEK_SET );

	http_param.host_name		= "www.rowebots.com";			//web site URL address
	http_param.host_ipaddr		= "184.107.235.10";			//web site ip address 
	http_param.host_path_file	= "/articles/U41-51compare.pdf";	//path to file at web site 
	http_param.host_port		= 80;
	http_param.max_file_size	= -1;					//max file size in bytes, which could be written.
										//value = -1, mean do not control size.

	xprintf("Start download file U41-51compare.pdf\n");
	/*************************************/
	/* HTTP Client GET */
	/*************************************/
	if((ret = http_client_get(&http_param, &result))< 0)
	{
		xprintf("Error http_client_get\n");
		switch(result)
		{
			case HTTPC_ENOMEM:
				xprintf("Not free memory\n");
			break;
			case HTTPC_GENERAL_TCP_ERROR:
				xprintf("TCP server error\n");
			break;
			case HTTPC_HOST_NOT_ANSWER:
				xprintf("Host not answer\n");
			break;
			case HTTPC_FILE_NOT_FOUND:
				xprintf("File not found on server\n");
			break;
			case HTTPC_ERROR_FILE_WRITE:
				xprintf("Error write to file\n");
			break;
		}
	}
	else
	{
		xprintf("File download! Size download file = %d\n",ret);
	}

	xprintf("File Closed\n");
	close(http_param.fd);

...

Example for HTTPS GET function:

	int ret;
	char result;
	http_get_param http_param;

	memset(&http_param, 0, sizeof(http_get_param));

	// Example  Download root HTML file from site wiki.com
	//Download Link: "http://wiki.com/"

	if(( http_param.fd = open("/dev/sd/index.html", O_CREAT | O_RDWR, 0)) == -1)
	{
		xprintf("files opened  errno = %d\n", errno);
	}
	lseek( http_param.fd, 0 , SEEK_SET );

	http_param.host_name		= "wiki.com";				//web site URL address
	http_param.host_ipaddr		= "66.96.160.149";					//web site ip address 
	http_param.host_path_file	= "/";	//path to file at web site 

	http_param.host_port		= 443;
	http_param.max_file_size	= -1;								//max file size in bytes, which could be written.
																	//value = -1, mean do not control size.
	http_param.SSL_param.SSL_cert_format = SSL_CERT_FMT_NONE;
	http_param.SSL_param.storage_format = memory_storage;
		
	http_param.SSL_param.storage.mem.SSL_key = (char *)target_key;
	http_param.SSL_param.storage.mem.SSL_key_len = sizeof(target_key);
	http_param.SSL_param.storage.mem.SSL_passwd = NULL;

	http_param.SSL_param.storage.mem.SSL_target_ss_cert = (char *)target_ss_cert;
	http_param.SSL_param.storage.mem.SSL_target_ss_cert_len = sizeof(target_ss_cert);
	http_param.SSL_param.storage.mem.SSL_host_ss_cert = (char *)host_ss_cert;
	http_param.SSL_param.storage.mem.SSL_host_ss_cert_len = sizeof(host_ss_cert);	
    
	xprintf("Start download file from https:/wiki.com/ \n");
	/*************************************/
	/* HTTPS Client GET */
	/*************************************/
	if((ret = https_client_get(&http_param, &result))< 0)
	{
		xprintf("Error http_client_get\n");
		switch(result)
		{
			case HTTPC_ENOMEM:
				xprintf("Not free memory\n");
			break;
			case HTTPC_GENERAL_TCP_ERROR:
				xprintf("TCP server error\n");
			break;
			case HTTPC_HOST_NOT_ANSWER:
				xprintf("Host not answer\n");
			break;
			case HTTPC_FILE_NOT_FOUND:
				xprintf("File not found on server\n");
			break;
			case HTTPC_ERROR_FILE_WRITE:
				xprintf("Error write to file\n");
			break;
		}
	}
	else
	{
		xprintf("File download! Size download file = %d\n",ret);
	}

	xprintf("File Closed\n");
	close(http_param.fd);

...

Example for HTTP POST function:

	int ret;
	http_post_param http_param;

	memset(&http_param, 0, sizeof(http_post_param));

	if(( http_param.fd = open(FATFS_MOUNT"/test.zip",O_RDONLY, 0)) == -1)
	{
		xprintf("Error: Files not found on SD(CF) or Error FAT-FS \n", errno);
		return;
	}

	http_param.file_size		= lseek(http_param.fd, 0, SEEK_END);
	// The offset is set 0
	lseek(http_param.fd, 0 , SEEK_SET );

	http_param.file_name		= "test.zip";			//file name to write to web server
	http_param.host_name		= "rowebots.com";		//web site URL address
	http_param.host_ipaddr		= "184.107.235.10";		//web site ip address
	http_param.host_path_file	= "/testing/uploadtest.php";	//path to file at web
	http_param.host_port		= 80;

	xprintf("Post to server =%s, file =%s\n",http_param.host_name,http_param.file_name);
	xprintf("file size =%d\n",http_param.file_size);

	/*************************************/
	/* HTTP Client Post */
	/*************************************/
	if((ret = http_client_post(&http_param,
			150000, // Timeout connect 4000 ms
			200000, // Total timeout 6000 ms
			NULL, NULL, 0 ))!= 0)
	{
		xprintf("Error http_client_post ret=%d\n",ret);
		switch(ret)
		{
			case HTTPC_ENOMEM:
				xprintf("Not free memory\n");
			break;
			case HTTPC_GENERAL_TCP_ERROR:
				xprintf("TCP server error\n");
			break;
			case HTTPC_HOST_NOT_ANSWER:
				xprintf("Host not answer\n");
			break;
			case HTTPC_ERROR_FILE_READ:
				xprintf("error read data from file\n");
			break;
			case HTTPC_TIMEOUT_UPLOAD:
				xprintf("TIMEOUT UPLOAD\n");
			break;
			case HTTPC_TIMEOUT_CONNECT:
				xprintf("TIMEOUT HTTPC_TIMEOUT_CONNECT\n");
			break;
		}
	}
	else
	{
		xprintf("File send Ok!\n");
	}

	close(http_param.fd);

...

 

NOTES

There are two demos available for the Unison and DSPnano HTTP Client which can be found in installdir/demos.

 

SEE ALSO

tcpd, tcpd_dual, fsys, fatfs, autoip

Suggest Edit

CONTACT US

TO GET YOUR PROJECT STARTED

TOP