/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/agent_return_data.h"
00033 #include "include/agent.h"
00034 
00035 agent_return_data_p
00036 agent_return_data_New(void)
00037 {
00038   agent_return_data_p agent_return_data;
00039   agent_return_data = (agent_return_data_p)malloc(sizeof(agent_return_data_t));
00040   CHECK_NULL(agent_return_data, exit(0););
00041   agent_return_data->data_type = 0;
00042   agent_return_data->array_dim = 0;
00043   agent_return_data->array_extent = NULL;
00044   agent_return_data->return_data = NULL;
00045   return agent_return_data;
00046 }
00047 
00048 agent_return_data_p 
00049 agent_return_data_InitializeFromAgent(agent_p agent)
00050 {
00051     int i;
00052     int size;
00053     int data_type_size;
00054     int progress;
00055     agent_return_data_t *agent_return;
00056     agent_return = (agent_return_data_t*)malloc(sizeof(agent_return_data_t));
00057     /* Get the array data type */
00058     agent_return->data_type = Ch_DataType(
00059             agent->agent_interp,
00060             agent->datastate->tasks[agent->datastate->task_progress]
00061             ->var_name );
00062     /* Get the array dimension */
00063     agent_return->array_dim = Ch_ArrayDim(
00064             agent->agent_interp,
00065             agent->datastate->tasks[agent->datastate->task_progress]
00066             ->var_name );
00067     /* Get the array extents */
00068     agent_return->array_extent = (int*)malloc(
00069             sizeof(int) * agent_return->array_dim );
00070     for (i=0; i<agent_return->array_dim; i++) {
00071         agent_return->array_extent[i] = 
00072             Ch_ArrayExtent(
00073                     agent->agent_interp,
00074                     agent->datastate
00075                     ->tasks[agent->datastate->task_progress]
00076                     ->var_name,
00077                     i );
00078     }
00079     /* Finally, allocate and point returnData to the right place. */
00080     size = 1;
00081     for (i=0; i < agent_return->array_dim; i++) {
00082         size *= agent_return->array_extent[i];
00083     }
00084 
00085     /* Now get the data type size */
00086     CH_DATATYPE_SIZE(agent_return->data_type, data_type_size);
00087 
00088     agent_return->return_data = (void*)malloc(size * data_type_size);
00089     CHECK_NULL(agent_return->return_data, exit(0));
00090     /* Copy the data over from the agent */
00091     /* For now, only support statically allocated global vars. */
00092     progress = agent->datastate->task_progress;
00093     i = 0;
00094 
00095     if (agent_return->array_dim == 0) {
00096         memcpy(
00097                 agent_return->return_data,
00098                 (void*)Ch_GlobalSymbolAddrByName(
00099                     agent->agent_interp,
00100                     agent->datastate->tasks[progress]->var_name),
00101                 size*data_type_size
00102               );
00103 
00104     } else {
00105         memcpy(
00106                 agent_return->return_data,
00107                 (void*)Ch_GlobalSymbolAddrByName(
00108                     agent->agent_interp,
00109                     agent->datastate->tasks[progress]->var_name),
00110                 size*data_type_size
00111               );
00112     }
00113 
00114     /* getAgentReturnArrays(
00115        agent_return->returnData,
00116        Ch_SymbolAddrByName(
00117        agent->agent_interp,
00118        agent->datastate->tasks[progress]->var_name),
00119        &i,
00120        agent_return->array_dim,
00121        agent_return->array_extent,
00122        agent_return->data_type ); */
00123 
00124     return agent_return;
00125 }
00126 
00127 int
00128 agent_return_data_Destroy(agent_return_data_p agent_return_data)
00129 {
00130   if (agent_return_data == NULL) {
00131     return MC_SUCCESS;
00132   }
00133   if (agent_return_data->array_extent != NULL) {
00134     free(agent_return_data->array_extent);
00135   }
00136   if (agent_return_data->return_data != NULL) {
00137     free(agent_return_data->return_data);
00138   }
00139   free(agent_return_data);
00140   return MC_SUCCESS;
00141 }
00142 

Generated on Fri May 16 14:49:54 2008 for Mobile-C by  doxygen 1.5.4