Back to home page

Enduro/X

 
 

    


0001 #!/usr/bin/pscript
0002 
0003 //
0004 // TODO: Change the linker options for OSX, etc... (RT Lib).
0005 //
0006 userlog("Generate go client");
0007 
0008 //Include wizard base.
0009 compilestring(getwizardbase())();
0010 
0011 //Provision class
0012 class CClient extends WizardBase {
0013 
0014         libs =  "-latmiclt -latmi -lubf -lnstd -lpthread -lrt -ldl -lm";
0015         
0016         constructor()
0017         {
0018                 base.constructor();
0019                 
0020                 if ("DARWIN"==getosname())
0021                 {
0022                         libs = "-latmiclt -latmi -lubf -lnstd -lpthread -ldl -lm";
0023                 }
0024         }
0025         
0026 
0027         //Configure it:
0028         </ order=0, name = "XATMI Client Name (binary)", type = "string", min=1, max=512 /> 
0029         cltname = "testcl";
0030         
0031         </ order=1, name = "Use UBF?", type = "yn"/> 
0032         useubf = "y";
0033         
0034         </ order=2, name = "UBF include folder name (will be done ../<name>)", 
0035                 type = "path" depend="(::prov.useubf==\"y\")" /> 
0036         ubfname = "ubftab";
0037         
0038         </ order=3, name = "Gen makefile", type = "yn"/> 
0039         genmake = "y";
0040         
0041         </ order=4, name = "INI File section (optional, will read config if set)", 
0042                 type = "string", depend="(::prov.useubf==\"y\")", min=0/> 
0043         config = "";
0044         
0045         goClientFile = "";
0046         
0047         makeFile = "";
0048         
0049         function getOutputFiles()
0050         {
0051                 goClientFile=appHome+"/"+cltname+".c";
0052                 makeFile=appHome+"/Makefile";
0053         }
0054         
0055         ////////////////////////////////////////////////////////////////////////
0056         //Build C Client code
0057         ////////////////////////////////////////////////////////////////////////
0058         cClientVal = "";
0059         function buildClient()
0060         {
0061         
0062                 cClientVal = @"#include <string.h>
0063 #include <stdio.h>
0064 #include <stdlib.h>
0065 #include <memory.h>
0066 #include <math.h>
0067 
0068 #include <ndebug.h>
0069 #include <atmi.h>
0070 
0071 "+(useubf=="n"?"":@"
0072 #include <ubf.h>
0073 #include <Exfields.h>
0074 #include <test.fd.h>")+@"
0075 /*---------------------------Externs------------------------------------*/
0076 /*---------------------------Macros-------------------------------------*/
0077 
0078 #ifndef SUCCEED
0079 #define SUCCEED                 0
0080 #endif
0081 
0082 #ifndef FAIL
0083 #define FAIL                    -1
0084 #endif
0085 
0086 "+(config==""?"":@"
0087 #define PROGSECTION             """+config+@""" /* configuration section */
0088 #define CONFIG_SERVER           ""@CCONF""
0089 #define KEY_VAL_BUFFSZ          1024
0090 ")+@"
0091 
0092 /*---------------------------Enums--------------------------------------*/
0093 /*---------------------------Typedefs-----------------------------------*/
0094 /*---------------------------Globals------------------------------------*/
0095 /*---------------------------Statics------------------------------------*/
0096 /*---------------------------Prototypes---------------------------------*/
0097 
0098 
0099 /**
0100  * Initialise the application
0101  * @param argc  argument count
0102  * @param argv  argument values
0103  * @return SUCCEED/FAIL
0104  */
0105 int init(int argc, char** argv)
0106 {
0107         int ret = SUCCEED;
0108 "+(config==""?"":@"
0109         UBFH *p_ub = NULL;
0110         char config_tag[128];
0111         long rsplen;
0112         BFLDLEN sz;
0113         int occ;
0114         int i;
0115         char key[KEY_VAL_BUFFSZ]={0};
0116         char val[KEY_VAL_BUFFSZ]={0};
0117         char *cctag;
0118         ")+@"
0119         TP_LOG(log_info, ""Initializing..."");
0120 
0121         if (SUCCEED!=tpinit(NULL))
0122         {
0123                 TP_LOG(log_error, ""Failed to Initialise: %s"", 
0124                         tpstrerror(tperrno));
0125                 ret = FAIL;
0126                 goto out;
0127         }
0128         
0129 "+(config==""?"":@"
0130         /* Download configuration */
0131         
0132         if (NULL==(p_ub = (UBFH *)tpalloc(""UBF"", NULL, 1024)))
0133         {
0134                 TP_LOG(log_error, ""Failed to alloc:%s"",  tpstrerror(tperrno));
0135                 ret=FAIL;
0136                 goto out;
0137         }
0138         
0139         cctag = getenv(""NDRX_CCTAG"");
0140         if (NULL!=cctag)
0141         {
0142                 snprintf(config_tag, sizeof(config_tag), ""%s/%s"", 
0143                         PROGSECTION, cctag);
0144         }
0145         else
0146         {
0147                 /* NO subsection configured */
0148                 snprintf(config_tag, sizeof(config_tag), ""%s"", PROGSECTION);
0149         }
0150         
0151         if ( (SUCCEED!=Bchg(p_ub, EX_CC_CMD, 0, ""g"", 0L))
0152                 || (SUCCEED!=Bchg(p_ub, EX_CC_LOOKUPSECTION, 0,  config_tag, 0L)))
0153         {
0154                 TP_LOG(log_error, ""Failed to set EX_CC_CMD/EX_CC_LOOKUPSECTION: %s"", 
0155                         Bstrerror(Berror));
0156                 ret = FAIL;
0157                 goto out;
0158         }
0159         
0160         if (FAIL==tpcall(CONFIG_SERVER, (char *)p_ub, 0L, (char **)&p_ub, &rsplen, TPNOTIME))
0161         {
0162                 TP_LOG(log_error, ""Failed to call %s: %s"", 
0163                          CONFIG_SERVER,tpstrerror(tperrno));
0164                 ret=FAIL;
0165                 goto out;
0166         }
0167         
0168         
0169         tplogprintubf(log_info, ""Got configuration"", p_ub);
0170 
0171         occ = Boccur(p_ub, EX_CC_KEY);
0172         
0173         for (i=0; i<occ; i++)
0174         {
0175                 sz = sizeof(key);
0176                 if (SUCCEED!=CBget(p_ub, EX_CC_KEY, i, key, &sz, BFLD_STRING))
0177                 {
0178                         TP_LOG(log_error, ""Failed to get EX_CC_KEY[%d]: %s"", i,
0179                              Bstrerror(Berror));
0180                         ret=FAIL;
0181                         goto out;
0182                 }
0183                 
0184                 sz = sizeof(val);
0185                 if (SUCCEED!=CBget(p_ub, EX_CC_VALUE, i, val, &sz, BFLD_STRING))
0186                 {
0187                         TP_LOG(log_error, ""Failed to get EX_CC_VALUE[%d]: %s"", i,
0188                              Bstrerror(Berror));
0189                         ret=FAIL;
0190                         goto out;
0191                 }
0192                 
0193                 TP_LOG(log_debug, ""Got key: [%s] = [%s]"",
0194                         key, val);
0195                 
0196                 if (0==strcmp(key, ""someparam1""))
0197                 {
0198                         TP_LOG(log_debug, ""Got param1: [%s]"", val);
0199                 }
0200                 else if (0==strcmp(key, ""someparam2""))
0201                 {
0202                         TP_LOG(log_debug, ""Got param2: [%s]"", val);
0203                 }
0204                 else
0205                 {
0206                         TP_LOG(log_debug, ""Unknown setting [%s] - ignoring..."",
0207                                 key
0208                         );
0209                 }
0210         }
0211         ")+@"
0212 
0213 out:
0214 "+(config==""?"":@"
0215         if (NULL!=p_ub)
0216         {
0217                 tpfree((char *)p_ub);
0218         }")+@"
0219         
0220         return ret;
0221 }
0222 
0223 /**
0224  * Terminate the application
0225  */
0226 int uninit(int status)
0227 {
0228         int ret = SUCCEED;
0229         
0230         TP_LOG(log_info, ""Uninitialising..."");
0231         
0232         ret = tpterm();
0233         
0234         return ret;
0235 }
0236 
0237 /**
0238  * Do processing (call some service)
0239  * @return SUCCEED/FAIL
0240  */
0241 int process (void)
0242 {
0243         int ret = SUCCEED;
0244         
0245 "+(useubf=="n"?"":@"
0246         UBFH *p_ub = NULL;
0247         long rsplen;
0248         char retbuf[128]={0};
0249         BFLDLEN sz;")+@"
0250         
0251         TP_LOG(log_info, ""Processing..."");
0252 
0253 "+(useubf=="n"?"":@"
0254         /* Call sample server... */
0255         
0256         if (NULL==(p_ub = (UBFH *)tpalloc(""UBF"", NULL, 1024)))
0257         {
0258                 TP_LOG(log_error, ""Failed to tpalloc: %s"",  tpstrerror(tperrno));
0259                 ret=FAIL;
0260                 goto out;
0261         }
0262         
0263         if (SUCCEED!=Bchg(p_ub, T_STRING_FLD, 0, ""Hello world!"", 0L))
0264         {
0265                 TP_LOG(log_error, ""Failed to set T_STRING_FLD: %s"",  
0266                       Bstrerror(Berror));
0267                 ret=FAIL;
0268                 goto out;
0269         }
0270         
0271         if (FAIL==tpcall(""TESTSV"", (char *)p_ub, 0L, (char **)&p_ub, &rsplen, TPNOTIME))
0272         {
0273                 TP_LOG(log_error, ""Failed to call TESTSV: %s"", tpstrerror(tperrno));
0274                 ret=FAIL;
0275                 goto out;
0276         }
0277         
0278         tplogprintubf(log_debug, ""Got response"", p_ub);
0279         
0280         
0281         sz = sizeof(retbuf);
0282         if (SUCCEED!=Bget(p_ub, T_STRING_2_FLD, 0, retbuf, &sz))
0283         {
0284                 TP_LOG(log_error, ""Failed to set T_STRING_2_FLD: %s"",  
0285                       Bstrerror(Berror));
0286                 ret=FAIL;
0287                 goto out;
0288         }
0289         
0290         TP_LOG(log_info, ""Got response from server: [%s]"", retbuf);")+@"
0291         
0292 out:
0293 
0294 "+(useubf=="n"?"":@"
0295         /* free up config data... */
0296         if (NULL!=p_ub)
0297         {
0298                 tpfree((char *)p_ub);
0299         }")+@"
0300         
0301         return ret;
0302 }
0303 
0304 /**
0305  * Main entry of th program
0306  * @param argc  argument count
0307  * @param argv  argument values
0308  * @return SUCCEED/FAIL
0309  */
0310 int main(int argc, char** argv)
0311 {
0312         int ret = SUCCEED;
0313 
0314         if (SUCCEED!=init(argc, argv))
0315         {
0316                 TP_LOG(log_error, ""Failed to Initialise!"");
0317                 ret=FAIL;
0318                 goto out;
0319         }
0320         
0321         
0322         if (SUCCEED!=process())
0323         {
0324                 TP_LOG(log_error, ""Process failed!"");
0325                 ret=FAIL;
0326                 goto out;
0327         }
0328     
0329 out:
0330         uninit(ret);
0331 
0332         return ret;
0333 }
0334 
0335 ";      
0336         }
0337 
0338         ////////////////////////////////////////////////////////////////////////
0339         //Client END
0340         ////////////////////////////////////////////////////////////////////////
0341         
0342         
0343         ////////////////////////////////////////////////////////////////////////
0344         //Build makefile
0345         ////////////////////////////////////////////////////////////////////////
0346         
0347         makeFileVal = "";
0348         function buildMakefile()
0349         {
0350         
0351                 makeFileVal = @"
0352 SOURCES = "+cltname+@".c
0353 TARGET  = "+cltname+@"
0354 OBJECTS = $(SOURCES:%.c=%.o)
0355 CFLAGS  = -I../"+ubfname+@"
0356 LIBS = "+libs+@"
0357 
0358 default: target
0359 
0360 target: $(TARGET)
0361 
0362 $(TARGET): $(OBJECTS)
0363         $(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
0364 
0365 %.o: %.c
0366         $(CC) -c -o $@ $< $(CFLAGS)
0367 
0368 clean:
0369         -rm *.o
0370         -rm $(TARGET)
0371 
0372 ";
0373         }
0374                 
0375         ////////////////////////////////////////////////////////////////////////
0376         //Build makefile, END
0377         ////////////////////////////////////////////////////////////////////////
0378         
0379 }
0380         
0381 
0382 //Run the mater installer
0383 function install() 
0384 {
0385         local root = getroottable();
0386 
0387         //Create a provision object
0388         root["prov"] <- CClient();
0389 
0390         if (!::prov.isDefaulted)
0391         {
0392                 ::prov.runInteractive();
0393         }
0394 
0395         if (::prov.validatAndPrintConfig())
0396         {
0397                 ::prov.getOutputFiles();
0398                 ::prov.buildClient();
0399 
0400                 if (!::prov.writeFile(::prov.goClientFile, ::prov.cClientVal))
0401                 {
0402                         return false;
0403                 }
0404                 
0405                 //Build makefile if needed...
0406                 if ("y"==::prov.genmake)
0407                 {
0408                         ::prov.buildMakefile();
0409                         
0410                         if (!::prov.writeFile(::prov.makeFile, ::prov.makeFileVal))
0411                         {
0412                                 return false;
0413                         }
0414                 }
0415         }
0416         else
0417         {
0418                 return false;
0419         }
0420 
0421         return true;
0422 }
0423 
0424 if (::install())
0425 {
0426         print("C client gen ok!\n");
0427         
0428         return 0;
0429 }
0430 else
0431 {
0432         print("C client gen failed!\n");
0433         return -1;
0434 }