javaextensions/bluetooth/bluetoothcommons/src.s60/bturlparams.cpp
changeset 21 2a9601315dfc
child 72 1f0034e370aa
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <cctype>    // tolower
       
    19 #include <locale>
       
    20 
       
    21 #include "logger.h"
       
    22 #include "bturlparams.h"
       
    23 #include "javacommonutils.h"
       
    24 #include "s60commonutils.h"
       
    25 #include "exceptionbase.h"
       
    26 #include "bluetoothconsts.h"
       
    27 
       
    28 using namespace std;
       
    29 using namespace java::util;
       
    30 using namespace java::bluetooth;
       
    31 
       
    32 OS_EXPORT BtUrlParams::BtUrlParams(std::wstring aUrl, std::wstring aFilter) :
       
    33         mMaster(false), mAuthorize(false), mAuthenticate(false), mEncrypt(false),
       
    34         mTransmitMtu(0), mReceiveMtu(0),mUrl(aUrl),mFilter(aFilter)
       
    35 {
       
    36     JELOG2(EJavaBluetooth);
       
    37 
       
    38     if (!mUrl.empty())
       
    39     {
       
    40         extractUrlParams(mUrl);
       
    41     }
       
    42 
       
    43     if (!mFilter.empty())
       
    44     {
       
    45         extractFilterParams(mFilter);
       
    46     }
       
    47 }
       
    48 
       
    49 BtUrlParams::BtUrlParams()
       
    50 {
       
    51 }
       
    52 
       
    53 OS_EXPORT BtUrlParams::~BtUrlParams()
       
    54 {
       
    55     std::list<HBufC*>::const_iterator it;
       
    56     for (it = mBlockedSenders.begin(); it != mBlockedSenders.end(); it++)
       
    57     {
       
    58         delete *it;
       
    59     }
       
    60 }
       
    61 
       
    62 OS_EXPORT std::wstring BtUrlParams::getProtocol()
       
    63 {
       
    64     return mProtocol;
       
    65 }
       
    66 
       
    67 OS_EXPORT std::wstring BtUrlParams::getServiceUuid()
       
    68 {
       
    69     return validLongUuid(mServiceUuid);
       
    70 }
       
    71 
       
    72 OS_EXPORT std::wstring BtUrlParams::getParamName()
       
    73 {
       
    74     return mName;
       
    75 }
       
    76 
       
    77 OS_EXPORT bool BtUrlParams::getParamMaster()
       
    78 {
       
    79     return mMaster;
       
    80 }
       
    81 
       
    82 OS_EXPORT bool BtUrlParams::getParamAuthorize()
       
    83 {
       
    84     return mAuthorize;
       
    85 }
       
    86 
       
    87 OS_EXPORT bool BtUrlParams::getParamAuthenticate()
       
    88 {
       
    89     return mAuthenticate;
       
    90 }
       
    91 
       
    92 OS_EXPORT bool BtUrlParams::getParamEncrypt()
       
    93 {
       
    94     return mEncrypt;
       
    95 }
       
    96 
       
    97 OS_EXPORT int BtUrlParams::getParamReceiveMtu()
       
    98 {
       
    99     return mReceiveMtu;
       
   100 }
       
   101 
       
   102 OS_EXPORT int BtUrlParams::getParamTransmitMtu()
       
   103 {
       
   104     return mTransmitMtu;
       
   105 }
       
   106 
       
   107 std::wstring BtUrlParams::validLongUuid(std::wstring &aStr)
       
   108 {
       
   109     int length = aStr.length();
       
   110     std::wstring validUuid(aStr);
       
   111 
       
   112     // Only if it is shorter than 8 bytes,
       
   113     // Then it will be added with the SHORT_BASE_UUID_STRING
       
   114     // Otherwise simply prepend zeros to the given uuid.
       
   115     if (length < 8)
       
   116     {
       
   117         std::wstring zeros = L"00000000";
       
   118         validUuid = zeros.substr(length);
       
   119         validUuid += aStr;
       
   120         validUuid += SHORT_UUID_BASE_STRING;
       
   121     }
       
   122     else if (length < 32)
       
   123     {
       
   124         std::wstring zeros=L"00000000000000000000000000000000";
       
   125         validUuid = zeros.substr(length);
       
   126         validUuid += aStr;
       
   127     }
       
   128     return validUuid;
       
   129 }
       
   130 
       
   131 std::wstring &BtUrlParams::ToLower(std::wstring &aStr)
       
   132 {
       
   133     JELOG2(EJavaBluetooth);
       
   134     for (size_t i = 0; i < aStr.length(); ++i)
       
   135         aStr[i] = std::tolower(aStr[i]);
       
   136     return aStr;
       
   137 }
       
   138 
       
   139 /**
       
   140  * extractUrlParams: Function to extract parameters from Server URL
       
   141  */
       
   142 
       
   143 void BtUrlParams::extractUrlParams(std::wstring aUrl)
       
   144 {
       
   145     JELOG2(EJavaBluetooth);
       
   146     LOG1(EJavaBluetooth, EInfo, "+ BtUrlParams::extractUrlParams %S",
       
   147          aUrl.c_str());
       
   148 
       
   149     int index = std::string::npos;
       
   150 
       
   151     // Extracting protocol
       
   152     index = aUrl.find(PROTOCOL_SEPERATOR);
       
   153 
       
   154     if (std::string::npos != index)
       
   155     {
       
   156         mProtocol = aUrl.substr(0, index);
       
   157         aUrl = aUrl.substr(index + wcslen(PROTOCOL_SEPERATOR));
       
   158         LOG1(EJavaBluetooth, EInfo,
       
   159              "  BtUrlParams::extractUrlParams Protocol=%S",
       
   160              mProtocol.c_str());
       
   161     }
       
   162 
       
   163     std::wstring tempUrl = aUrl.substr(0, wcslen(LOCALHOST));
       
   164     if (0 == ToLower(tempUrl).compare(LOCALHOST))
       
   165     {
       
   166         aUrl = aUrl.substr(wcslen(LOCALHOST));
       
   167     }
       
   168 
       
   169     LOG1(EJavaBluetooth, EInfo, "  BtUrlParams::extractUrlParams url=%S",
       
   170          aUrl.c_str());
       
   171 
       
   172     // Extracting Service UUID
       
   173     index = aUrl.find(SEMICOLON);
       
   174     if (std::string::npos != index)
       
   175     {
       
   176         mServiceUuid = aUrl.substr(0, index);
       
   177         aUrl = aUrl.substr(index);
       
   178     }
       
   179     else
       
   180     {
       
   181         mServiceUuid = aUrl;
       
   182     }
       
   183 
       
   184     if (aUrl.length() <= 1)
       
   185     {
       
   186         return;
       
   187     }
       
   188 
       
   189     // Extracting other parameters
       
   190     index = aUrl.find(SEMICOLON, 1);
       
   191 
       
   192     int eqLength = wcslen(EQUAL);
       
   193     while (std::string::npos != index)
       
   194     {
       
   195         int eqIndex = std::string::npos;
       
   196 
       
   197         eqIndex = aUrl.find(EQUAL);
       
   198         if (std::string::npos != eqIndex && eqIndex < index)
       
   199         {
       
   200             setUrlParamsValue(aUrl.substr(0, eqIndex + eqLength),
       
   201                               aUrl.substr(eqIndex + eqLength, index - (eqIndex
       
   202                                           + eqLength)));
       
   203         }
       
   204         else
       
   205         { // Actually erroneous URL !!
       
   206             break;
       
   207         }
       
   208         aUrl = aUrl.substr(index);
       
   209         LOG1(EJavaBluetooth, EInfo,
       
   210              "  BtUrlParams::extractUrlParams In while: %S", aUrl.c_str());
       
   211 
       
   212         if (aUrl.length() <= 1)
       
   213         {
       
   214             break;
       
   215         }
       
   216         index = aUrl.find(SEMICOLON, 1);
       
   217     }
       
   218 
       
   219     // Extracting last parameter: In case URL is not ended with ";"
       
   220     if (!aUrl.empty())
       
   221     {
       
   222         LOG1(EJavaBluetooth, EInfo,
       
   223              "  BtUrlParams::extractUrlParams After while loop: %S",
       
   224              aUrl.c_str());
       
   225         int eqIndex = std::string::npos;
       
   226 
       
   227         eqIndex = aUrl.find(EQUAL);
       
   228         if (std::string::npos != eqIndex)
       
   229         {
       
   230             setUrlParamsValue(aUrl.substr(0, eqIndex + eqLength),
       
   231                               aUrl.substr(eqIndex + eqLength, index - (eqIndex
       
   232                                           + eqLength)));
       
   233         }
       
   234     }
       
   235 }
       
   236 
       
   237 void BtUrlParams::setUrlParamsValue(std::wstring aParam, std::wstring aVal)
       
   238 {
       
   239     JELOG2(EJavaBluetooth);
       
   240     LOG2(EJavaBluetooth, EInfo,
       
   241          "+ BtUrlParams::setUrlParamsValue Param:%S Val:%S", aParam.c_str(),
       
   242          aVal.c_str());
       
   243     ToLower(aParam);
       
   244     if (0 == aParam.compare(URL_PARAM_AUTHENTICATE))
       
   245     {
       
   246         mAuthenticate = ToLower(aVal).compare(L"false");
       
   247     }
       
   248     else if (0 == aParam.compare(URL_PARAM_AUTHORIZE))
       
   249     {
       
   250         mAuthorize = ToLower(aVal).compare(L"false");
       
   251     }
       
   252     else if (0 == aParam.compare(URL_PARAM_ENCRYPT))
       
   253     {
       
   254         mEncrypt = ToLower(aVal).compare(L"false");
       
   255     }
       
   256     else if (0 == aParam.compare(URL_PARAM_MASTER))
       
   257     {
       
   258         mMaster = ToLower(aVal).compare(L"false");
       
   259     }
       
   260     else if (0 == aParam.compare(URL_PARAM_NAME))
       
   261     {
       
   262         mName = aVal;
       
   263     }
       
   264     else if (0 == aParam.compare(URL_PARAM_RECEIVE_MTU))
       
   265     {
       
   266         try
       
   267         {
       
   268             LOG2(EJavaBluetooth, EInfo,
       
   269                  "+ BtUrlParams::setUrlParamsValue URL_PARAM_RECEIVE_MTU:%S Val:%S",
       
   270                  aParam.c_str(), aVal.c_str());
       
   271             mReceiveMtu = JavaCommonUtils::wstringToInt(aVal);
       
   272         }
       
   273         catch (ExceptionBase ex)
       
   274         {
       
   275             ELOG1(EJavaBluetooth,
       
   276                   "- ServiceRecord::setUrlParamsValue URL_PARAM_RECEIVE_MTU exception Caught: %S",
       
   277                   ex.toString().c_str());
       
   278         }
       
   279     }
       
   280     else if (0 == aParam.compare(URL_PARAM_TRANSMIT_MTU))
       
   281     {
       
   282         try
       
   283         {
       
   284             LOG2(EJavaBluetooth, EInfo,
       
   285                  "+ BtUrlParams::setUrlParamsValue URL_PARAM_TRANSMIT_MTU:%S Val:%S",
       
   286                  aParam.c_str(), aVal.c_str());
       
   287             mTransmitMtu = JavaCommonUtils::wstringToInt(aVal);
       
   288         }
       
   289         catch (ExceptionBase ex)
       
   290         {
       
   291             ELOG1(EJavaBluetooth,
       
   292                   "- ServiceRecord::setUrlParamsValue URL_PARAM_TRANSMIT_MTU exception Caught: %S",
       
   293                   ex.toString().c_str());
       
   294         }
       
   295     }
       
   296     else
       
   297     {
       
   298         // Nothing to do
       
   299     }
       
   300 }
       
   301 
       
   302 /**
       
   303  * extractUrlParams: Function to extract parameters from Push Filter
       
   304  */
       
   305 
       
   306 void BtUrlParams::extractFilterParams(std::wstring aFilter)
       
   307 {
       
   308     JELOG2(EJavaBluetooth);
       
   309     LOG1(EJavaBluetooth, EInfo, "+ BtUrlParams::extractFilterParams %S",
       
   310          aFilter.c_str());
       
   311     int index = std::string::npos;
       
   312 
       
   313     // Extracting black list items (if any)
       
   314     index = aFilter.find(FILTER_PARAM_BLACKLIST);
       
   315 
       
   316     if (std::string::npos != index)
       
   317     {
       
   318         fillBlackList(aFilter.substr(index + wcslen(FILTER_PARAM_BLACKLIST)));
       
   319         aFilter = aFilter.substr(0, index);
       
   320     }
       
   321 
       
   322     LOG2(EJavaBluetooth, EInfo,
       
   323          "+ BtUrlParams::extractFilterParams Allowed List:(%d)%S", index,
       
   324          aFilter.c_str());
       
   325 
       
   326     // Extracting Allowed list
       
   327     index = aFilter.find(SEMICOLON);
       
   328     LOG2(EJavaBluetooth, EInfo,
       
   329          "+ BtUrlParams::extractFilterParams Allowed List \";\" (%d)%S",
       
   330          index, aFilter.c_str());
       
   331 
       
   332     if (std::string::npos != index)
       
   333     {
       
   334         mAllowedSenders.mPattern = aFilter.substr(0, index - wcslen(SEMICOLON));
       
   335         LOG1(EJavaBluetooth, EInfo,
       
   336              "  BtUrlParams::extractFilterParams AllowedSender Pattern:%S",
       
   337              mAllowedSenders.mPattern.c_str());
       
   338     }
       
   339     else
       
   340     {
       
   341         mAllowedSenders.mPattern = aFilter;
       
   342         LOG1(EJavaBluetooth, EInfo,
       
   343              "  BtUrlParams::extractFilterParams AllowedSender Pattern:%S",
       
   344              mAllowedSenders.mPattern.c_str());
       
   345     }
       
   346 
       
   347     // Extracting Authenticated
       
   348     index = aFilter.find(FILTER_PARAM_AUTHENTICATED);
       
   349     LOG2(
       
   350         EJavaBluetooth,
       
   351         EInfo,
       
   352         "+ BtUrlParams::extractFilterParams Allowed List \"authenticated\" (%d)%S",
       
   353         index, aFilter.c_str());
       
   354     if (std::string::npos != index)
       
   355     {
       
   356         mAllowedSenders.mAuthenticated = true;
       
   357         LOG(EJavaBluetooth, EInfo,
       
   358             "  BtUrlParams::extractFilterParams Authenticated");
       
   359     }
       
   360     else
       
   361     {
       
   362         mAllowedSenders.mAuthenticated = false;
       
   363     }
       
   364 
       
   365     // Extracting Authorized
       
   366     index = aFilter.find(FILTER_PARAM_AUTHORIZED);
       
   367     LOG2(
       
   368         EJavaBluetooth,
       
   369         EInfo,
       
   370         "+ BtUrlParams::extractFilterParams Allowed List \"authorized\" (%d)%S",
       
   371         index, aFilter.c_str());
       
   372     if (std::string::npos != index)
       
   373     {
       
   374         mAllowedSenders.mAuthorized = true;
       
   375         LOG(EJavaBluetooth, EInfo,
       
   376             "  BtUrlParams::extractFilterParams Authorized");
       
   377     }
       
   378     else
       
   379     {
       
   380         mAllowedSenders.mAuthorized = false;
       
   381     }
       
   382 }
       
   383 
       
   384 void BtUrlParams::fillBlackList(std::wstring aBlackList)
       
   385 {
       
   386     JELOG2(EJavaBluetooth);
       
   387     int index = std::string::npos;
       
   388 
       
   389     index = aBlackList.find(SEMICOLON);
       
   390 
       
   391     while (std::string::npos != index)
       
   392     {
       
   393         std::wstring pattern;
       
   394 
       
   395         pattern = aBlackList.substr(0, index - wcslen(SEMICOLON));
       
   396         aBlackList = aBlackList.substr(index + wcslen(SEMICOLON));
       
   397         LOG1(EJavaBluetooth, EInfo,
       
   398              "  BtUrlParams::fillBlackList Adding Pattern:%S",
       
   399              pattern.c_str());
       
   400         try
       
   401         {
       
   402             HBufC *blackPattern = S60CommonUtils::wstringToDes(
       
   403                                       pattern.c_str());
       
   404             mBlockedSenders.push_back(blackPattern);
       
   405             index = aBlackList.find(SEMICOLON);
       
   406         }
       
   407         catch (ExceptionBase ex)
       
   408         {
       
   409             // Nothing to handle here.
       
   410         }
       
   411     }
       
   412 
       
   413     // Extracting last parameter: In case URL is not ended with ";"
       
   414     if (!aBlackList.empty())
       
   415     {
       
   416         LOG1(EJavaBluetooth, EInfo,
       
   417              "  BtUrlParams::fillBlackList Adding Last Pattern:%S",
       
   418              aBlackList.c_str());
       
   419         try
       
   420         {
       
   421             HBufC *blackPattern = S60CommonUtils::wstringToDes(
       
   422                                       aBlackList.c_str());
       
   423             mBlockedSenders.push_back(blackPattern);
       
   424         }
       
   425         catch (ExceptionBase ex)
       
   426         {
       
   427             // Nothing to handle here.
       
   428         }
       
   429     }
       
   430 }
       
   431 
       
   432 OS_EXPORT bool BtUrlParams::isAllowedSender(HBufC& aDevAddr, bool &aAuthorized,
       
   433         bool &aAuthenticated)
       
   434 {
       
   435     JELOG2(EJavaBluetooth);
       
   436     int matchFlag = false;
       
   437 
       
   438     LOG2(EJavaBluetooth, EInfo,
       
   439          "  BtUrlParams::isAllowedSender Device:%S  Pattern:%S",
       
   440          aDevAddr.Des().PtrZ(), mAllowedSenders.mPattern.c_str());
       
   441 
       
   442     matchFlag = matchWS(aDevAddr, mAllowedSenders.mPattern);
       
   443 
       
   444     if (matchFlag == true)
       
   445     {
       
   446         aAuthorized = mAllowedSenders.mAuthorized;
       
   447         aAuthenticated = mAllowedSenders.mAuthenticated;
       
   448     }
       
   449     return matchFlag;
       
   450 }
       
   451 
       
   452 OS_EXPORT bool BtUrlParams::isBlockedSender(HBufC& aDevAddr)
       
   453 {
       
   454     JELOG2(EJavaBluetooth);
       
   455     int matchFlag = false;
       
   456     std::list<HBufC*>::const_iterator cii;
       
   457 
       
   458     for (cii = mBlockedSenders.end(); cii != mBlockedSenders.begin();)
       
   459     {
       
   460         if (matchHB(aDevAddr, **(--cii)))
       
   461             return true;
       
   462     }
       
   463     return matchFlag;
       
   464 }
       
   465 
       
   466 bool BtUrlParams::matchWS(HBufC& aDevAddr, std::wstring aPattern)
       
   467 {
       
   468     JELOG2(EJavaBluetooth);
       
   469     HBufC *pattern;
       
   470     try
       
   471     {
       
   472         pattern = S60CommonUtils::wstringToDes(aPattern.c_str());
       
   473     }
       
   474     catch (ExceptionBase ex)
       
   475     {
       
   476         return true;
       
   477     }
       
   478 
       
   479     bool ret = matchHB(aDevAddr, *pattern);
       
   480     delete pattern;
       
   481     return ret;
       
   482 }
       
   483 
       
   484 bool BtUrlParams::matchHB(HBufC &aDevAddr, HBufC &aPattern)
       
   485 {
       
   486     JELOG2(EJavaBluetooth);
       
   487 
       
   488     if (aDevAddr.MatchF(aPattern) >= 0)
       
   489         return true;
       
   490     return false;
       
   491 }