/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/agent_return_data.c

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

Generated on Fri Jul 11 17:59:44 2008 for Mobile-C by  doxygen 1.5.4