/home/dko/projects/mobilec/trunk/src/agent_return_data.c

Go to the documentation of this file.
00001 /*[
00002  * Copyright (c) 2007 Integration Engineering Laboratory
00003                       University of California, Davis
00004  *
00005  * Permission to use, copy, and distribute this software and its
00006  * documentation for any purpose with or without fee is hereby granted,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.
00010  *
00011  * Permission to modify the software is granted, but not the right to
00012  * distribute the complete modified source code.  Modifications are to
00013  * be distributed as patches to the released version.  Permission to
00014  * distribute binaries produced by compiling modified sources is granted,
00015  * provided you
00016  *   1. distribute the corresponding source modifications from the
00017  *    released version in the form of a patch file along with the binaries,
00018  *   2. add special version identification to distinguish your version
00019  *    in addition to the base release version number,
00020  *   3. provide your name and address as the primary contact for the
00021  *    support of your modified version, and
00022  *   4. retain our contact information in regard to use of the base
00023  *    software.
00024  * Permission to distribute the released version of the source code along
00025  * with corresponding source modifications in the form of a patch file is
00026  * granted with same provisions 2 through 4 for binary distributions.
00027  *
00028  * This software is provided "as is" without express or implied warranty
00029  * to the extent permitted by applicable law.
00030 ]*/
00031 
00032 #include "include/interpreter_variable_data.h"
00033 #include "include/agent.h"
00034 
00035 interpreter_variable_data_p
00036 interpreter_variable_data_New(void)
00037 {
00038   interpreter_variable_data_p agent_variable_data;
00039   agent_variable_data = (interpreter_variable_data_p)malloc(sizeof(interpreter_variable_data_t));
00040   CHECK_NULL(agent_variable_data, exit(0););
00041   agent_variable_data->name = NULL;
00042   agent_variable_data->size = 0;
00043   agent_variable_data->data_type = 0;
00044   agent_variable_data->array_dim = 0;
00045   agent_variable_data->array_extent = NULL;
00046   agent_variable_data->data = NULL;
00047   return agent_variable_data;
00048 }
00049 
00050 interpreter_variable_data_p 
00051 interpreter_variable_data_InitializeFromAgent(agent_p agent)
00052 {
00053     int i;
00054     int size;
00055     int data_type_size;
00056     int progress;
00057     interpreter_variable_data_t *agent_return;
00058     agent_return = (interpreter_variable_data_t*)malloc(sizeof(interpreter_variable_data_t));
00059     agent_return->name = strdup(
00060             agent->datastate->tasks[agent->datastate->task_progress]
00061             ->var_name );
00062 
00063     /* Get the array data type */
00064     agent_return->data_type = Ch_DataType(
00065             agent->agent_interp,
00066             agent->datastate->tasks[agent->datastate->task_progress]
00067             ->var_name );
00068     /* Get the array dimension */
00069     agent_return->array_dim = Ch_ArrayDim(
00070             agent->agent_interp,
00071             agent->datastate->tasks[agent->datastate->task_progress]
00072             ->var_name );
00073     /* Get the array extents */
00074     agent_return->array_extent = (int*)malloc(
00075             sizeof(int) * agent_return->array_dim );
00076     for (i=0; i<agent_return->array_dim; i++) {
00077         agent_return->array_extent[i] = 
00078             Ch_ArrayExtent(
00079                     agent->agent_interp,
00080                     agent->datastate
00081                     ->tasks[agent->datastate->task_progress]
00082                     ->var_name,
00083                     i );
00084     }
00085     /* Finally, allocate and point returnData to the right place. */
00086     size = 1;
00087     for (i=0; i < agent_return->array_dim; i++) {
00088         size *= agent_return->array_extent[i];
00089     }
00090 
00091     /* Now get the data type size */
00092     CH_DATATYPE_SIZE(agent_return->data_type, data_type_size);
00093 
00094     agent_return->data = (void*)malloc(size * data_type_size);
00095     CHECK_NULL(agent_return->data, exit(0));
00096     /* Copy the data over from the agent */
00097     /* For now, only support statically allocated global vars. */
00098     progress = agent->datastate->task_progress;
00099     i = 0;
00100 
00101     if (agent_return->array_dim == 0) {
00102         memcpy(
00103                 agent_return->data,
00104                 (void*)Ch_GlobalSymbolAddrByName(
00105                     agent->agent_interp,
00106                     agent->datastate->tasks[progress]->var_name),
00107                 size*data_type_size
00108               );
00109 
00110     } else {
00111         memcpy(
00112                 agent_return->data,
00113                 (void*)Ch_GlobalSymbolAddrByName(
00114                     agent->agent_interp,
00115                     agent->datastate->tasks[progress]->var_name),
00116                 size*data_type_size
00117               );
00118     }
00119     agent_return->size = size*data_type_size;
00120 
00121     /* getAgentReturnArrays(
00122        agent_return->returnData,
00123        Ch_SymbolAddrByName(
00124        agent->agent_interp,
00125        agent->datastate->tasks[progress]->var_name),
00126        &i,
00127        agent_return->array_dim,
00128        agent_return->array_extent,
00129        agent_return->data_type ); */
00130 
00131     return agent_return;
00132 }
00133 
00134 interpreter_variable_data_p
00135 interpreter_variable_data_Initialize(agent_p agent, const char* varname)
00136 {
00137   int i;
00138   int size;
00139   int data_type_size;
00140   interpreter_variable_data_t *interp_variable;
00141 
00142   /* Make sure the agent is not running */
00143   MUTEX_LOCK(agent->run_lock);
00144 
00145   interp_variable = (interpreter_variable_data_t*)malloc(sizeof(interpreter_variable_data_t));
00146   interp_variable->name = strdup(varname);
00147 
00148   /* Get the array data type */
00149   interp_variable->data_type = Ch_DataType(
00150       agent->agent_interp,
00151       varname );
00152   /* Get the array dimension */
00153   interp_variable->array_dim = Ch_ArrayDim(
00154       agent->agent_interp,
00155       varname );
00156   /* Get the array extents */
00157   interp_variable->array_extent = (int*)malloc(
00158       sizeof(int) * interp_variable->array_dim );
00159   for (i=0; i<interp_variable->array_dim; i++) {
00160     interp_variable->array_extent[i] = 
00161       Ch_ArrayExtent(
00162           agent->agent_interp,
00163           varname,
00164           i );
00165   }
00166   /* Finally, allocate and point returnData to the right place. */
00167   size = 1;
00168   for (i=0; i < interp_variable->array_dim; i++) {
00169     size *= interp_variable->array_extent[i];
00170   }
00171 
00172   /* Now get the data type size */
00173   CH_DATATYPE_SIZE(interp_variable->data_type, data_type_size);
00174 
00175   interp_variable->data = (void*)malloc(size * data_type_size);
00176   CHECK_NULL(interp_variable->data, exit(0));
00177   /* Copy the data over from the agent */
00178   /* For now, only support statically allocated global vars. */
00179   i = 0;
00180 
00181   if (interp_variable->array_dim == 0) {
00182     memcpy(
00183         interp_variable->data,
00184         (void*)Ch_GlobalSymbolAddrByName(
00185           agent->agent_interp,
00186           varname),
00187         size*data_type_size
00188         );
00189 
00190   } else {
00191     memcpy(
00192         interp_variable->data,
00193         (void*)Ch_GlobalSymbolAddrByName(
00194           agent->agent_interp,
00195           varname),
00196         size*data_type_size
00197         );
00198   }
00199   interp_variable->size = size*data_type_size;
00200 
00201   MUTEX_UNLOCK(agent->run_lock);
00202   return interp_variable;
00203 }
00204 
00205 int
00206 interpreter_variable_data_Destroy(interpreter_variable_data_p agent_variable_data)
00207 {
00208   if (agent_variable_data == NULL) {
00209     return MC_SUCCESS;
00210   }
00211   if (agent_variable_data->name != NULL) {
00212     free(agent_variable_data->name);
00213   }
00214   if (agent_variable_data->array_extent != NULL) {
00215     free(agent_variable_data->array_extent);
00216   }
00217   if (agent_variable_data->data != NULL) {
00218     free(agent_variable_data->data);
00219   }
00220   free(agent_variable_data);
00221   return MC_SUCCESS;
00222 }
00223 
00224 interpreter_variable_data_p
00225 interpreter_variable_data_Copy(interpreter_variable_data_p src)
00226 {
00227   struct interpreter_variable_data_s* tmp;
00228   tmp = interpreter_variable_data_New();
00229   if(src->name != NULL) 
00230     tmp->name = strdup(src->name);
00231   tmp->size = src->size;
00232   tmp->data_type = src->data_type;
00233   tmp->array_dim = src->array_dim;
00234   if(src->array_extent != NULL) {
00235     tmp->array_extent = (int*)malloc(sizeof(int)*src->array_dim);
00236     memcpy(tmp->array_extent, src->array_extent, sizeof(int)*src->array_dim);
00237   }
00238   if(src->data != NULL) {
00239     tmp->data = memcpy(tmp->array_extent, src->array_extent, src->size);
00240   }
00241 
00242   return tmp;
00243 }

Generated on Mon Jun 23 16:01:09 2008 for Mobile-C by  doxygen 1.5.4