joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.c Source File

main.c

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <arpa/inet.h>
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <errno.h>
00021 #include <pthread.h>
00022 #include <string.h>
00023 #include "sn_nsdl.h"
00024 #include "sn_coap_header.h"
00025 #include "sn_coap_protocol.h"
00026 #include "sn_nsdl_lib.h"
00027 #include "ns_list.h"
00028 #include "arguments.h"
00029 
00030 #define NUMTHREADS 10
00031 
00032 extern int register_endpoint(int port, sn_nsdl_ep_parameters_s *endpoint_ptr, int thread_id);
00033 void stop_pgm(char *s)
00034 {
00035     perror(s);
00036     exit(1);
00037 }
00038 
00039 void *own_alloc(uint16_t size)
00040 {
00041     if(size) {
00042         return malloc(size);
00043     }
00044     else {
00045         return 0;
00046     }
00047 }
00048 
00049 void own_free(void *ptr)
00050 {
00051     free(ptr);
00052 }
00053 
00054 void arg_init(void)
00055 {
00056     memcpy(arg_dst,"::1",32);   //default localhost
00057     arg_sport=5683;
00058     arg_dport=5683;
00059 }
00060 
00061 
00062 void usage_show(void)
00063 {
00064     printf("Usage:\n\n"
00065 
00066             "multithread-linux-test [-d 127.0.0.1] \n"
00067             "-d NSP IPv4 address (default = ::1)\n"
00068             "-dp NSP port number (default = 5683)\n");
00069 }
00070 
00071 /* function to be executed by the new thread */
00072 void* create_endpoint(void *arg)
00073 {    
00074     int index = *((int *) arg);
00075     int port = 8100 + index;
00076     sn_nsdl_ep_parameters_s *endpoint_ptr;
00077     uint8_t endpoint_type[] = {"type"};
00078     uint8_t lifetime_ptr[] = {"120"};
00079     char str[10];
00080     sprintf(str, "THREAD_%d", index);
00081     endpoint_ptr = own_alloc(sizeof(sn_nsdl_ep_parameters_s));
00082     if(endpoint_ptr)
00083     {
00084         memset(endpoint_ptr, 0, sizeof(sn_nsdl_ep_parameters_s));
00085         endpoint_ptr->endpoint_name_ptr = str;
00086         endpoint_ptr->endpoint_name_len = strlen(str);
00087         endpoint_ptr->type_ptr = endpoint_type;
00088         endpoint_ptr->type_len =  sizeof(endpoint_type)-1;
00089         endpoint_ptr->lifetime_ptr = lifetime_ptr;
00090         endpoint_ptr->lifetime_len =  sizeof(lifetime_ptr)-1;
00091     }
00092     register_endpoint(port, endpoint_ptr, index);
00093     if(endpoint_ptr) {
00094         own_free(endpoint_ptr);
00095         endpoint_ptr = 0;
00096     }    
00097 }
00098 
00099 int main(int argc, char **argv)
00100 {    
00101     uint8_t i;
00102     arg_init();
00103 
00104     if (argc<1)
00105     {
00106         usage_show();
00107     }
00108     else
00109     {
00110         i=1; //argv[0] is the command itself
00111 
00112         argc--; //get the real number of arguments
00113         while (i<=argc)
00114         {
00115             //check arguments
00116             if (!(strcmp("-h",argv[i])))
00117             {
00118                 usage_show();
00119                 stop_pgm("");
00120             }
00121             else if (!(strcmp("-d",argv[i])))
00122             {
00123                 if (i++==argc) stop_pgm("Argument missed for option -d\n");
00124                 memcpy(arg_dst,argv[i],strlen((const char*)argv[i])+1);
00125                 i++;
00126                 continue;
00127             }
00128             else if (!(strcmp("-p",argv[i])))
00129             {
00130                 if (i++==argc) stop_pgm("Argument missed for option -p\n");
00131                 arg_port=atoi(argv[i]);
00132                 i++;
00133                 continue;
00134             }
00135             else if (!(strcmp("-dp",argv[i])))
00136             {
00137                 if (i++==argc) stop_pgm("Argument missed for option -dp\n");
00138                 arg_dport=atoi(argv[i]);
00139                 i++;
00140                 continue;
00141             }
00142             else
00143             {
00144                 usage_show();
00145                 stop_pgm("\n--- Argument error ---\n");
00146             }
00147 
00148         }
00149     }
00150 
00151     pthread_t threads[NUMTHREADS];
00152     for (int index = 0; index < NUMTHREADS; index++) {        
00153         pthread_create(&threads[index], NULL, create_endpoint, (void *) &index);        
00154         sleep(1);
00155     }
00156 
00157     for (int i = 0; i < NUMTHREADS; i) {
00158         pthread_join(threads[i], NULL);
00159     }
00160     exit(0);
00161 }
00162 
00163 
00164