osptoolkit.c

Go to the documentation of this file.
00001 /*
00002  * openser osp module. 
00003  *
00004  * This module enables openser to communicate with an Open Settlement 
00005  * Protocol (OSP) server.  The Open Settlement Protocol is an ETSI 
00006  * defined standard for Inter-Domain VoIP pricing, authorization
00007  * and usage exchange.  The technical specifications for OSP 
00008  * (ETSI TS 101 321 V4.1.1) are available at www.etsi.org.
00009  *
00010  * Uli Abend was the original contributor to this module.
00011  * 
00012  * Copyright (C) 2001-2005 Fhg Fokus
00013  *
00014  * This file is part of Kamailio, a free SIP server.
00015  *
00016  * Kamailio is free software; you can redistribute it and/or modify
00017  * it under the terms of the GNU General Public License as published by
00018  * the Free Software Foundation; either version 2 of the License, or
00019  * (at your option) any later version
00020  *
00021  * Kamailio is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  * GNU General Public License for more details.
00025  *
00026  * You should have received a copy of the GNU General Public License
00027  * along with this program; if not, write to the Free Software
00028  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00029  */
00030 
00031 #include <osp/osptrans.h>
00032 #include "../../dprint.h"
00033 #include "osptoolkit.h"
00034 
00035 static OSPTTHREADRETURN ospReportUsageWork(void* usagearg);
00036 
00037 typedef struct _osp_usage
00038 {
00039     OSPTTRANHANDLE ospvTransaction;     /* Transaction handle */
00040     unsigned ospvReleaseCode;           /* Release code */
00041     unsigned ospvDuration;              /* Length of call */
00042     time_t ospvStartTime;               /* Call start time */
00043     time_t ospvEndTime;                 /* Call end time */
00044     time_t ospvAlertTime;               /* Call alert time */
00045     time_t ospvConnectTime;             /* Call connect time */
00046     unsigned ospvIsPDDInfoPresent;      /* Is PDD Info present */
00047     unsigned ospvPostDialDelay;         /* Post Dial Delay */
00048     unsigned ospvReleaseSource;         /* EP that released the call */
00049 } osp_usage;
00050 
00051 /*
00052  * Get OSP transaction ID from transaction handle
00053  * param transaction OSP transaction headle
00054  * return OSP transaction ID
00055  */
00056 unsigned long long ospGetTransactionId(
00057     OSPTTRANHANDLE transaction)
00058 {
00059     OSPTTRANS* context = NULL;
00060     unsigned long long id = 0;
00061     int errorcode = OSPC_ERR_NO_ERROR;
00062 
00063     context = OSPPTransactionGetContext(transaction, &errorcode);
00064 
00065     if (errorcode == OSPC_ERR_NO_ERROR) {
00066         id = (unsigned long long)context->TransactionID;
00067     } else {
00068         LM_ERR("failed to extract transaction_id from transaction handle %d (%d)\n",
00069             transaction,
00070             errorcode);
00071     }
00072 
00073     return id;
00074 }
00075 
00076 /*
00077  * Create a thread to report OSP usage
00078  * param ospvTransaction OSP transaction handle
00079  * param ospvReleaseCode Call release reason
00080  * param ospvDurating Call duration
00081  * param ospvStartTime Call start time
00082  * param ospvEndTime Call end time
00083  * param ospvAlertTime Call alert time
00084  * param ospvConnectTime Call connected  time
00085  * param ospvIsPDDInfoPresent If post dial delay information avaliable
00086  * param ospvPostDialDelay Post dial delay information
00087  * param ospvReleaseSource Which side release the call
00088  */
00089 void ospReportUsageWrapper(
00090     OSPTTRANHANDLE ospvTransaction,
00091     unsigned ospvReleaseCode,
00092     unsigned ospvDuration,
00093     time_t ospvStartTime,
00094     time_t ospvEndTime,
00095     time_t ospvAlertTime,
00096     time_t ospvConnectTime,
00097     unsigned ospvIsPDDInfoPresent,
00098     unsigned ospvPostDialDelay,
00099     unsigned ospvReleaseSource)
00100 {
00101     osp_usage* usage;
00102     OSPTTHREADID threadid;
00103     OSPTTHRATTR threadattr;
00104     int errorcode;
00105 
00106     LM_DBG("schedule usage report for '%llu'\n", ospGetTransactionId(ospvTransaction));
00107 
00108     usage = (osp_usage*)malloc(sizeof(osp_usage));
00109 
00110     usage->ospvTransaction = ospvTransaction;
00111     usage->ospvReleaseCode = ospvReleaseCode;
00112     usage->ospvDuration = ospvDuration;
00113     usage->ospvStartTime = ospvStartTime;
00114     usage->ospvEndTime = ospvEndTime;
00115     usage->ospvAlertTime = ospvAlertTime;
00116     usage->ospvConnectTime = ospvConnectTime;
00117     usage->ospvIsPDDInfoPresent = ospvIsPDDInfoPresent;
00118     usage->ospvPostDialDelay = ospvPostDialDelay;
00119     usage->ospvReleaseSource = ospvReleaseSource;
00120 
00121     OSPM_THRATTR_INIT(threadattr, errorcode);
00122 
00123     OSPM_SETDETACHED_STATE(threadattr, errorcode);
00124 
00125     OSPM_CREATE_THREAD(threadid, &threadattr, ospReportUsageWork, usage, errorcode);
00126 
00127     OSPM_THRATTR_DESTROY(threadattr);
00128 }
00129 
00130 /*
00131  * Report OSP usage thread function
00132  * param usagearg OSP usage information
00133  * return
00134  */
00135 static OSPTTHREADRETURN ospReportUsageWork(
00136     void* usagearg)
00137 {
00138     int i;
00139     const int MAX_RETRIES = 5;
00140     osp_usage* usage;
00141     int errorcode;
00142 
00143     usage = (osp_usage*)usagearg;
00144 
00145     OSPPTransactionRecordFailure(
00146         usage->ospvTransaction,
00147         (enum OSPEFAILREASON)usage->ospvReleaseCode);
00148 
00149     for (i = 1; i <= MAX_RETRIES; i++) {
00150         errorcode = OSPPTransactionReportUsage(
00151             usage->ospvTransaction,
00152             usage->ospvDuration,
00153             usage->ospvStartTime,
00154             usage->ospvEndTime,
00155             usage->ospvAlertTime,
00156             usage->ospvConnectTime,
00157             usage->ospvIsPDDInfoPresent,
00158             usage->ospvPostDialDelay,
00159             usage->ospvReleaseSource,
00160             (unsigned char*)"", 0, 0, 0, 0, NULL, NULL);
00161 
00162         if (errorcode == OSPC_ERR_NO_ERROR) {
00163             LM_DBG("reporte usage for '%llu'\n", 
00164                 ospGetTransactionId(usage->ospvTransaction));
00165             break;
00166         } else {
00167             LM_ERR("failed to report usage for '%llu' (%d) attempt '%d' of '%d'\n",
00168                 ospGetTransactionId(usage->ospvTransaction), 
00169                 errorcode,
00170                 i,
00171                 MAX_RETRIES);
00172         }
00173     }
00174 
00175     OSPPTransactionDelete(usage->ospvTransaction);
00176 
00177     free(usage);
00178 
00179     OSPTTHREADRETURN_NULL();
00180 }

Generated on Thu May 24 00:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6