/*
 * Copyright 2013 Anton Tananaev (anton.tananaev@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.protocol;

import java.nio.charset.Charset;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder; 

public class TotemFrameDecoder extends FrameDecoder {

    @Override
    protected Object decode(
            ChannelHandlerContext ctx,
            Channel channel,
            ChannelBuffer buf) throws Exception {
        
        // Check minimum length
        if (buf.readableBytes() < 10) {
            return null;
        }
        
        // Trim end line
        if (buf.getUnsignedShort(buf.readerIndex()) == 0x0d0a) {
            buf.skipBytes(2);
        }
        
        if(buf.toString(buf.readerIndex(), 2, Charset.defaultCharset()).equals("$$")){
            int length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, Charset.defaultCharset()), 16);
            if (length <= buf.readableBytes()) {
                return buf.readBytes(length);
            }
        }else{
            //TODO: notify to user the GPS response
            return buf.readBytes(buf.readableBytes());
        }
        
        return null;
    }

}

@@ -877,17 +877,62 @@
 Decoder;
- 
+%0Aimport org.traccar.helper.ChannelBufferTools;
 %0A%0Apublic
@@ -1141,24 +1141,16 @@
 ption %7B%0A
-        
 %0A       
@@ -1245,32 +1245,24 @@
 ;%0A        %7D%0A
-        
 %0A        // 
@@ -1265,135 +1265,161 @@
  // 
-Trim end line%0A        if (buf.getUnsignedShort(buf.readerIndex()) == 0x0d0a) %7B%0A            buf.skipBytes(2);%0A  
+Find start%0A        Integer beginIndex = ChannelBufferTools.find(buf, buf.readerIndex(), %22$$%22);%0A        if (beginIndex == null) %7B%0A
      
- %7D%0A
        
- 
+return null;
 %0A   
@@ -1427,24 +1427,32 @@
     
+%7D else 
 if
+ 
 (b
-uf.toString(
+eginIndex %3E 
 buf.
@@ -1468,58 +1468,88 @@
 ex()
-, 2, Charset.defaultCharset()).equals(%22$$%22))%7B%0A    
+) %7B%0A            buf.readerIndex(beginIndex);%0A        %7D%0A%0A        // Read message%0A
     
@@ -1657,28 +1657,24 @@
 6);%0A        
-    
 if (length %3C
@@ -1702,36 +1702,32 @@
 ) %7B%0A            
-    
 return buf.readB
@@ -1744,144 +1744,8 @@
 h);%0A
-            %7D%0A        %7Delse%7B%0A            //TODO: notify to user the GPS response%0A            return buf.readBytes(buf.readableBytes());%0A
     
@@ -1750,24 +1750,16 @@
       %7D%0A
-        
 %0A       

Fix Totem frame decoder